RFR: 8328821: Map.of().entrySet() mutators should throw UnsupportedOperationException

Chen Liang liach at openjdk.org
Mon May 27 12:21:03 UTC 2024


On Mon, 27 May 2024 07:24:47 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> This change overrides mutator methods in the implementation returned by `Map.of().entrySet()` to throw `UnsupportedOperationException`.
>
> src/java.base/share/classes/java/util/ImmutableCollections.java line 1323:
> 
>> 1321:                 @Override
>> 1322:                 public int hashCode() {
>> 1323:                     return MapN.this.hashCode();
> 
> The hash code for a `Set` is defined as the sum of the elements in the `Set` (hash(`null`) == 0). The `Map. Entry` hash code is defined the same way `MapN.this.hashCode` operates so this seems right. It would be nice with a couple of tests that assert this invariant.
> 
> Perhaps the existing tests already cover this?

Good point, this is currently missing from MOAT in Collections.

Though it can be as simple as one line here:
https://github.com/openjdk/jdk/blob/891d5aedf12e837c9a9c7cb800fb3affa7430f00/test/jdk/java/util/Collection/MOAT.java#L1329

check(m.hashCode() == m.entrySet().hashCode());

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

PR Review Comment: https://git.openjdk.org/jdk/pull/18522#discussion_r1615976036


More information about the core-libs-dev mailing list