equals-hashcode: equals/hashCode contract demos, missing hashCode and constant-hash costs, mutable keys, records, JPA entity id trap, 13 contract tests
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b66uwsWr9B1ouyrzUh
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
== what the JDK uses ==
|
||||
"hello".hashCode() = 99162322
|
||||
31*h + c over "hello" = 99162322
|
||||
List.of(1,2,3).hashCode() = 30817
|
||||
Arrays.hashCode({1,2,3}) = 30817
|
||||
Objects.hash(1,2,3) = 30817
|
||||
Set.of(1,2,3).hashCode() = 6 (sum of elements)
|
||||
Map.of(1,2).hashCode() = 3 (key^value per entry, summed)
|
||||
|
||||
== Objects.hash(...) traps ==
|
||||
Objects.hash("a") vs "a".hashCode() = 128 vs 97
|
||||
Objects.hash(1, 2) == Objects.hash(2, 1) ? false
|
||||
Objects.hash() (no args) = 1
|
||||
Objects.hash((Object) null) = 31
|
||||
|
||||
== Arrays as fields ==
|
||||
x.equals(y) = false, Arrays.equals = true
|
||||
x.hashCode() == y.hashCode() ? false
|
||||
Arrays.hashCode equal ? true
|
||||
|
||||
== equal hashCodes do not mean equal objects ==
|
||||
"Aa".hashCode() = 2112, "BB".hashCode() = 2112, equal strings? false
|
||||
Reference in New Issue
Block a user