-
Sub-task
-
Resolution: Delivered
-
P4
-
19
New static factory methods have been introduced to allow creation of `HashMap` and related instances that are preallocated to accommodate an expected number of mappings or elements. After using the `HashMap.newHashMap` method, the requested number of mappings can be added to the newly created `HashMap` without it being resized. There are similar new static factory methods for `LinkedHashMap`, `WeakHashMap`, `HashSet`, and `LinkedHashSet`. The complete set of new methods is:
* HashMap.newHashMap
* LinkedHashMap.newLinkedHashMap
* WeakHashMap.newWeakHashMap
* HashSet.newHashSet
* LinkedHashSet.newLinkedHashSet
The int-argument constructors for these classes set the "capacity" (internal table size) which is not the same as the number of elements that can be accommodated. The capacity is related to the number of elements by a simple but error-prone calculation. For this reason, programs should use these new static factory methods in preference to the int-argument constructors.
* HashMap.newHashMap
* LinkedHashMap.newLinkedHashMap
* WeakHashMap.newWeakHashMap
* HashSet.newHashSet
* LinkedHashSet.newLinkedHashSet
The int-argument constructors for these classes set the "capacity" (internal table size) which is not the same as the number of elements that can be accommodated. The capacity is related to the number of elements by a simple but error-prone calculation. For this reason, programs should use these new static factory methods in preference to the int-argument constructors.
- relates to
-
JDK-8284780 Need methods to create pre-sized HashSet and LinkedHashSet
- Resolved