Red black tree implementation in Hashmap of java 8 without comparator

Viewed 352

Treemap uses red-black tree internally for implementation. Treemap takes Comparable<> or Comparator<> as parameter which red-black tree uses for inserting data in binary search tree.

From java 8, hash map has started using red-black tree once threeshold of linklist is reached in case of hash collision. My question is, for custom class I implement equals() and hashcode() but not comparator and use this custom class as key in hashmap then how red black tree will work without comparator in case of hash collision.

1 Answers

Summarizing my understanding for other viewers of this question:

  1. If there is bucket collision(which is not necessarily hash code collision), then binary tree comes in picture and searches for hashcode
  2. if hash code is same then it will rely on comparable implementation of key of hashmap.
  3. If key has not implemented comparable then both(left and right) branches of tree will be traversed in this scenario.
Related