Java HashMap get works but containsKey does not

后端 未结 9 1288
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 05:41

I am trying to locate a key in a HashMap. I can print the selected key by using \'get\' but when I use \'containsKey\' in an if statement, it is not found.

I KNOW

9条回答
  •  隐瞒了意图╮
    2020-12-06 06:22

    Take a peak at the source code for the HashMap implementation. Both get and containsKey use the hasCode() and equals() methods of your key object.

    The only real difference, and as was pointed out, it is a trivial null check, is in the comparisons:

    get:

    ((k = e.key) == key || key.equals(k))
    

    containsKey:

    ((k = e.key) == key || (key != null && key.equals(k)))
    

    where e is of type Entry for a HashMap.

    So, if you do not have a strong implementations of hashCode() and/or equals() you will have a problem. Additionally, if your keys were mutated (I see that you did not declare the class fields final) you could have an issue.

    Take the following example:

    public class HashMapTest {
        static class KeyCheck {
            int value;
            public KeyCheck(int value) { this.value = value; }
            public void setValue(int value) { this.value = value; }
            @Override public int hashCode() { return value; }
            @Override public boolean equals(Object o) {
                return ((KeyCheck)o).value == this.value;
            }
        }
    
        public static void main(String args[]) {
            HashMap map = new HashMap();
            KeyCheck k1 = new KeyCheck(5);
            KeyCheck k2 = new KeyCheck(5);
    
            map.put(k1, "Success");
    
            System.out.println("Key: " + k1 + " Get: " + map.get(k1) +
                               " Contains: " + map.containsKey(k1));
            System.out.println("Key: " + k2 + " Get: " + map.get(k2) +
                               " Contains: " + map.containsKey(k2));
    
            k1.setValue(10);
    
            System.out.println("Key: " + k1 + " Get: " + map.get(k1) +
                               " Contains: " + map.containsKey(k1));
            System.out.println("Key: " + k2 + " Get: " + map.get(k2) +
                               " Contains: " + map.containsKey(k2));
        }
    }
    

    This will print out:

    Key: HashMapTest$KeyCheck@5 Get: Success Contains: true
    Key: HashMapTest$KeyCheck@5 Get: Success Contains: true
    Key: HashMapTest$KeyCheck@a Get: null Contains: false
    Key: HashMapTest$KeyCheck@5 Get: null Contains: false

    As you can see, in this case the mutability caused the hashCode() to change, which ruined everything.

提交回复
热议问题