On Fri, 12 Jun 2026 15:59:53 GMT, Chen Liang <[email protected]> wrote:
>> src/java.base/share/classes/java/util/WeakHashMap.java line 59:
>>
>>> 57: * to hold the key cannot refer to a value object.
>>> 58: * Methods such as {@linkplain #get get} or {@linkplain
>>> #containsKey containsKey}
>>> 59: * will always return {@code null} or {@code false}
>>> respectively.
>>
>> I think this assumes a working equals method. A Dr. Evil crafted value class
>> with a cunning equals/hashCode would lead to get/containKey matching an
>> existing entry. I can mull over improvement this sentence.
>
> True, and that isn't necessarily a "Dr. Evil" - it's a valid use for users to
> construct a cheap lookup key. However, I don't have any good clue on
> improving this particular sentence.
There are a few other issues here. The paragraph in the class description about
weak reference is further down so it's a bit surprising to have the preview
note link to WeakReference before this. Also the implementation note has always
used "value objects" when speaking about the map values so it is confusing.
To keep it simple, I think the preview note should be after the first paragraph
(before revealing that null keys and values are allowed) and need only say that
the value objects cannot be used as keys and some words about methods to put
mappings throw IdentityException. We don't need to enumerate all the methods.
We can dodge the concerns about wonky equals methods by just dropping the
sentence on get/containsKey. And we can fix the implNote to replace the use of
"value objects".
I can create a PR for this.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/31123#discussion_r3421925693