RFR: 8186958: Need method to create pre-sized HashMap [v5]
Roger Riggs
rriggs at openjdk.java.net
Thu Mar 24 15:00:46 UTC 2022
On Thu, 24 Mar 2022 12:10:28 GMT, XenoAmess <duke at openjdk.java.net> wrote:
>> 8186958: Need method to create pre-sized HashMap
>
> XenoAmess has updated the pull request incrementally with one additional commit since the last revision:
>
> delete a space.
src/java.base/share/classes/java/util/HashMap.java line 2549:
> 2547:
> 2548: /**
> 2549: * Calculate initial capacity for HashMap based classes, from expected size.
This method should say it is using a default load factor (0.75).
I'm not sure how often anyone creates a map with a non-default load factor
but that might suggest a more general API pattern either in the supporting calculation
or the public factories.
src/java.base/share/classes/java/util/HashMap.java line 2556:
> 2554: */
> 2555: static int calculateHashMapCapacity(int expectedSize) {
> 2556: if (expectedSize >= 1610612736) {
It would be clearer with a comment on the constant or use the expression (the compiler will evaluate it).
`Integer.MAX_VALUE / 4 * 3`.
There also might be a valid point to make about a fail-fast exception if the requested capacity cannot be achieved. If defaulted to MAX_VALUE, the application would start populating the map and then at some later point get an exception when it cannot be resized to accommodate the n+1'th item.
src/java.base/share/classes/java/util/HashMap.java line 2565:
> 2563: * Creates a new, empty HashMap with an initial table size
> 2564: * accommodating the specified number of elements without the need
> 2565: * to dynamically resize.
The default load factor should be mentioned.
-------------
PR: https://git.openjdk.java.net/jdk/pull/7928
More information about the core-libs-dev
mailing list