Why is the equals method not called in a hashmap?

import java.util.*;
class Dog {
    public Dog(String n) { name = n; }
    public String name;
    public boolean equals(Object o) {
        System.out.println("equals called..");
        if((o instanceof Dog) &&
                (((Dog)o).name == name)) {
            return true;
        } else {
            return false;
        }
    }
    public int hashCode() {
        System.out.println("hashCodecalled..");
        return name.length(); 
    }
}
class SampleClass {
    public static void main(String[] args) {
        Map<Object, String> m = new HashMap<Object, String>();
        Dog d1 = new Dog("clover");
        m.put(d1, "Dog key");
        d1.name ="arthur";
        System.out.println(m.get(d1));      
    }
}

In the above code, the output I get is:

hashCodecalled..
hashCodecalled..
Dog key

after i do d1.name = "arthur"

i expected the output to be

hashCodecalled..``
hashCodecalled..
equals called..
null
+4
source share
8 answers

Since it HashMaphas the exact object you are looking for, it does not need to be called equalsto make sure the object is correct.

When you getobject out HashMap, it is first evaluated hashCodeto find the right bucket. Then a bucket search is performed. But each key in the bucket is compared using ==. If the object is not the one requested, it is used equals.

In Java 7, the key part of the code in the method get HashMapis

if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
    return e.value;
+5

.

hashCode : " equals (Object), hashCode ". , , equals, , hashcode.

HashMap hashCode . , , , -, hashcode . (. , .)

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Map<Object, String> m = new HashMap<Object, String>();
    Dog d1 = new Dog("clover");
    Dog d2 = new Dog("clover 2");
    m.put(d1, "Dog 1");
    m.put(d2, "Dog 2");
    System.out.println(m.get(d1)); 
}

class Dog {
private String name;
public Dog(String n) {   
    name = n; 
}

@Override
public boolean equals(final Object o) {
    System.out.println("equals called..");
    return true;
}

@Override
public int hashCode() {
    System.out.println("hashCodecalled..");
    return 1; // constant
}}

:

hashCodecalled..

hashCodecalled..

.

hashCodecalled..

2

, , , , .

+2

. java.util.HashMap.get.

, == e.key key.equals(e.key).

get. , == e.key. key.equals(e.key) .

public V get(Object key) {
    if (key == null)
        return getForNullKey();
    int hash = hash(key.hashCode());
    for (Entry<K,V> e = table[indexFor(hash, table.length)];
      e != null;
      e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
            return e.value;
    }
    return null;
}
+1

HashMap, null, hashcode ( ), == ( ). , equals() , .

 public V get(Object key) {
    if (key == null)
        return getForNullKey();
    int hash = hash(key.hashCode());
    for (Entry<K,V> e = table[indexFor(hash, table.length)];
         e != null;
         e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
            return e.value;
    }
    return null;
}
+1

Mine - HashMap , .

HashMap , ( ). , hashCode, . , , Dog k1 = new Dog("clover"), Dog k2 = new Dog("clever") Dog k3 = new Dog("123456"); - hashCode() Dog hashCode. 3- 6, .

get(), HashMap - , , , , - , < t27 > .

, , ( Dog) () - HashMap, / ( ). , .

1:

, Dog k3 name "1234", - 4. , k1, k2 k3 HashMap, get(k3) ( "1234" ) , hashCode 4. - , hashCodes 6 hashCode 4, , equals().

"", , , .

get() null. , , , hashCode 6.

2:

k3 name "654321" , 6, , equals() 3 , , equals() true , k3. , k3, , , HashMap, . k3, equals() ( , , , !). get() . , hashCode 6.

3:

, Dog k4 = new Dog("123456");, k3.name "654321". , , get(k4)! , == - , , 3 , , get (k4) false. k3.name , , "123456" "654321" . "123456" , "123456" , , "654321" .

, . HashSet a Set, . Infact, HashSet HashMap .

:

. bucketing , , , ( ) . , (get()), HashMap, . bucketing, , / .

, Hash, / hashCode() equals(), , . JavaDoc , .

PS: == equals() . , .

+1

, hashCode name String, , , 6.

HashMap , equals(). equals, .

0

-, . , -, , , , .

0

Map (, String -), , Map (- ).

You can make an immutable user class (for example, Dog), so you cannot change the key used later.

It is like you lock the door with a key, and later you change the same key with some β€œtools” and then expect to open the locked door.

0
source

Source: https://habr.com/ru/post/1546771/


All Articles