liveBook · Manning

Hash collision

As explained in Clojure: The Essential Reference

A hash collision occurs when distinct keys produce the same hash-table index or hash value.

A hash collision occurs when distinct keys produce the same hash value or hash-table index. Hashing quality matters because frequent collisions can make composite-key maps inefficient. section 6.6

Java hashCode can create easy collisions for values such as longs, vectors, or sets. Clojure’s hash function accounts for these cases, reducing frequent collisions for numbers and small collections used as keys. section 6.6

Composite keys with colliding Java hashes

Several long values produce repeated Java hashCode results: (-3 -2 -1 0 1 2) becomes (2 1 0 0 1 2). Two distinct composite sets both produce 2274. Applying Clojure’s hash instead produces distinct results for the sets: (2055406432 -916052234).

When working with keys and hashing:

  • Prefer Clojure hashing for Clojure collections used as map keys when frequent Java hashCode collisions would be a concern. section 6.6

How Clojure: The Essential Reference covers it

How deep it goes
Contrasts Java hashCode with Clojure hash through concrete outputs, but stops short of explaining hash-table bucket algorithms.
Examples
Uses colliding longs and composite sets, then compares Java hashCode outputs with Clojure hash outputs.
What you can do after
Evaluate key-hashing behavior and choose Clojure collection hashing when Java hashCode produces frequent collisions.
How it connects
Connects hash collisions with hash functions, composite keys, hash maps, equality, Java hashCode, and collection hashing.

Also explained in

Compare how 4 books explain hash collision →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.