Skip to content

Instantly share code, notes, and snippets.

@jeffque
Created August 25, 2025 12:02
Show Gist options
  • Select an option

  • Save jeffque/febdf793ae2edeb1167e3fbcfe328ee8 to your computer and use it in GitHub Desktop.

Select an option

Save jeffque/febdf793ae2edeb1167e3fbcfe328ee8 to your computer and use it in GitHub Desktop.
Testing whether IdentityMap relies on hashCode somehow
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