TreeMap.get() return Null even key exists

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

    this code here is not working if the class word don't override properly Equals and hashcode...

    TreeMap test = new TreeMap<>();
    test.put(new Word("pqr",12),1);
    test.put(new Word("abc",2),1);
    
    Integer prq = test.get(new Word("pqr",1));
    System.out.println(prq);
    prq = test.get(new Word("pqr",12));
    

提交回复
热议问题