A hash collision occurs when distinct objects produce the same hash value. HashMap and HashSet may treeify a heavily colliding bucket, using compareTo() alongside hashCode() and equals(). section 8.6
A crafted StringBuilder collision
A demonstration creates 40 StringBuilder objects in one bucket, finds another object with a matching hash code, inserts it, and then mutates it. After mutation, contains(sb) returns false because the set can no longer locate the changed key. section 8.6
Avoid collision-related collection failures by:
- Keeping objects used as HashMap keys or HashSet elements immutable while stored. section 8.6
- Treating identity-based hash codes as implementation-dependent; collision distributions are not guaranteed by the JVM specification. section 8.6



