TreeMap.get() return Null even key exists

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

    This is what exactly happen in the TreeMpa.get() method,

    while (p != null) {
                int cmp = k.compareTo(p.key);
                if (cmp < 0)
                    p = p.left;
                else if (cmp > 0)
                    p = p.right;
                else
                    return p;
            }
    

    TreeMpa use compareTo method to find the value. so it will loop through all the item till k.compareTo(p.key) return 0

提交回复
热议问题