mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8186958: Need method to create pre-sized HashMap
Reviewed-by: chegar, naoto, joehw, lancea, wetmore, smarks
This commit is contained in:
parent
41fc078323
commit
87faa85c59
30 changed files with 208 additions and 73 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -433,6 +433,10 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||
* Constructs an empty {@code HashMap} with the specified initial
|
||||
* capacity and load factor.
|
||||
*
|
||||
* @apiNote
|
||||
* To create a {@code HashMap} with an initial capacity that accommodates
|
||||
* an expected number of mappings, use {@link #newHashMap(int) newHashMap}.
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @param loadFactor the load factor
|
||||
* @throws IllegalArgumentException if the initial capacity is negative
|
||||
|
@ -455,6 +459,10 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||
* Constructs an empty {@code HashMap} with the specified initial
|
||||
* capacity and the default load factor (0.75).
|
||||
*
|
||||
* @apiNote
|
||||
* To create a {@code HashMap} with an initial capacity that accommodates
|
||||
* an expected number of mappings, use {@link #newHashMap(int) newHashMap}.
|
||||
*
|
||||
* @param initialCapacity the initial capacity.
|
||||
* @throws IllegalArgumentException if the initial capacity is negative.
|
||||
*/
|
||||
|
@ -2545,4 +2553,32 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate initial capacity for HashMap based classes, from expected size and default load factor (0.75).
|
||||
*
|
||||
* @param numMappings the expected number of mappings
|
||||
* @return initial capacity for HashMap based classes.
|
||||
* @since 19
|
||||
*/
|
||||
static int calculateHashMapCapacity(int numMappings) {
|
||||
return (int) Math.ceil(numMappings / (double) DEFAULT_LOAD_FACTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new, empty HashMap suitable for the expected number of mappings.
|
||||
* The returned map uses the default load factor of 0.75, and its initial capacity is
|
||||
* generally large enough so that the expected number of mappings can be added
|
||||
* without resizing the map.
|
||||
*
|
||||
* @param numMappings the expected number of mappings
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
* @return the newly created map
|
||||
* @throws IllegalArgumentException if numMappings is negative
|
||||
* @since 19
|
||||
*/
|
||||
public static <K, V> HashMap<K, V> newHashMap(int numMappings) {
|
||||
return new HashMap<>(calculateHashMapCapacity(numMappings));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue