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
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 Word
s 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.