How does Java 8's HashMap degenerate to balanced trees when many keys have the same hash code?

前端 未结 3 495
礼貌的吻别
礼貌的吻别 2020-12-01 07:57

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

3条回答
  •  孤街浪徒
    2020-12-01 08:42

    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)

提交回复
热议问题