How does Java 8\'s HashMap degenerate to balanced trees when many keys have the same hash code? I read that keys should implement Comparable to define an order
Comparable
Read the code. It is mostly a red-black tree.
It does not actually require the implementation of Comparable, but can use it if available (see for instance the find method)