TreeMap.get() return Null even key exists

前端 未结 5 1496
我寻月下人不归
我寻月下人不归 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:00

    Since your Word class implements Comparable interface . So TreeMap uses compareTo method for comparing properties of objects.

    If you want to get the object with same String "word" then implement compareTo method like

    @Override
    public int compareTo(Word o) {
        return this.word.compareTo(o.word);
    }
    

提交回复
热议问题