Created
August 25, 2025 12:02
-
-
Save jeffque/febdf793ae2edeb1167e3fbcfe328ee8 to your computer and use it in GitHub Desktop.
Testing whether IdentityMap relies on hashCode somehow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jeffque; | |
| import java.util.IdentityHashMap; | |
| import java.util.Objects; | |
| public class IdentityTest { | |
| static class SomeValue { | |
| int x; | |
| SomeValue(int x) { | |
| this.x = x; | |
| } | |
| @Override | |
| public int hashCode() { | |
| return Objects.hashCode(x); | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (obj instanceof SomeValue other) { | |
| return this.x == other.x; | |
| } | |
| return false; | |
| } | |
| } | |
| public static void main(String[] args) { | |
| final var idMap = new IdentityHashMap<SomeValue, String>(); | |
| final var k = new SomeValue(1); | |
| idMap.put(k, "first"); | |
| k.x = 42; | |
| System.out.println(idMap.get(k)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment