TreeMap.get() return Null even key exists

前端 未结 5 1461
我寻月下人不归
我寻月下人不归 2021-01-23 18:49

I am trying to get from TreeMap but it return null even the key exist. HashCode and eqauls is based on word only. Comparable is based on freqency.

    public sta         


        
5条回答
  •  孤街浪徒
    2021-01-23 19:14

    TreeMap uses the compareTo method to decide key ordering and key equality.

    Since your Word class uses the freq field in its compareTo method, any two Words with the same freq are considered equal.

    From the documentation of TreeMap:

    ... but a sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal.

提交回复
热议问题