RFR: 8281631: HashMap copy constructor and putAll can over-allocate table [v28]

Roger Riggs rriggs at openjdk.java.net
Thu Mar 10 22:41:43 UTC 2022


On Thu, 10 Mar 2022 22:01:49 GMT, Stuart Marks <smarks at openjdk.org> wrote:

>> @stuart-marks please have a look in changes in the latest commit, I think we'd better to manually create references for keys like that.
>
> Good point about WeakHashMap! I don't think we need a separate table. Since the value is held by a strong reference, using the same reference as the key will prevent the key's weak reference from being cleared. I'd suggest this:
> 
>     for (int i = 0; i < n; i++) {
>         Integer obj = i; // prevent WeakHashMap from clearing keys
>         map.put(obj, obj);
>     }

Heads up here!
java.lang.Integer is specified  as a value based class and should not be used where identity is needed.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/doc-files/ValueBased.html

I don't have a ready suggestion, but be forewarned that this use is not considered valid.

-------------

PR: https://git.openjdk.java.net/jdk/pull/7431


More information about the core-libs-dev mailing list