Java HashMap get works but containsKey does not

后端 未结 9 1283
没有蜡笔的小新
没有蜡笔的小新 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:19

    As descibed here, you have to override the equals(Object) method.

    The reason why get(Object) is working is, that HashMap will calculate the Hash for your Location class and returns the Object the hascode points to.

    containsKey(Object) calculates the hash key and gets the object the hash is pointed to. The object from the HashMap will compare to the Object you put in. For these comparison the equals method is used. When you do not override he equals method, true is returned, when the object reference to the same instance.

    From HashMap

    /** 
     * Check for equality of non-null reference x and possibly-null y. 
     */
    static boolean eq(Object x, Object y) {
        return x == y || x.equals(y);
    }
    

    From Object

    public boolean equals(Object obj) {
        return (this == obj);
        }
    

    From the javadoc of equals

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

    0 讨论(0)
  • 2020-12-06 06:19

    i think sometime you need the hash code and sometimes not so i think in this way you can turn of the hash code checking when you want buy changing the hash code for all objects you want to 0

    public class sample(){
        @JsonIgnore
        private int hashCode = super.hashCode();
    
        public void setHashCode(int hashCode){
            this.hashCode = hashCode;
        }    
    
        @Override
        public int hashCode(){
            return this.hashCode;
        }    
    
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final ReflectObject other = (ReflectObject) obj;
            if (this.hashCode != other.hashCode) {
                return false;
            }
            return true;
        }
    }
    
    0 讨论(0)
  • 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<KeyCheck, String> map = new HashMap<KeyCheck, String>();
            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.

    0 讨论(0)
提交回复
热议问题