RFR: 8186958: Need method to create pre-sized HashMap [v7]
liach
duke at openjdk.java.net
Sat Mar 26 12:54:42 UTC 2022
On Sat, 26 Mar 2022 12:14:25 GMT, XenoAmess <duke at openjdk.java.net> wrote:
>> src/java.base/share/classes/java/util/HashMap.java line 2584:
>>
>>> 2582:
>>> 2583: /**
>>> 2584: * Creates a new, empty HashMap with an initial table size
>>
>> You probably wanna allow for a non-NEW instance for the corner case where the given size is 0 - no elements.
>
>> You probably wanna allow for a non-NEW instance for the corner case where the given size is 0 - no elements.
>
> @ChrisHegarty I guess we shouldn't.
>
> I want to make it 100% equals to `new HashMap()` constructor, thus migrate all usecases.
>
> So if we apply this, and when the original usage use this map object as a lock, or put some elements after the call(sometimes people cannot decide if they would really put elements in this map), bad things would happen.
>
> Besides, there already a function` Map.of()` for such functionality, so programmer should use that instead if they really want a shared empty map.
hash maps are modifiable. empty instances can be changed, and returning such shared instances are inherently unsafe.
-------------
PR: https://git.openjdk.java.net/jdk/pull/7928
More information about the core-libs-dev
mailing list