Like hashmap using hashtable to store and retrieve an object

I know that hashmap actually uses hashcode to store and retrieve an object from hashtable, but my doupt is the hash code that it uses. In fact, the map contains a hash code for the key and hashcode for its value. consider this method

Map<String,String> student=new HashMap();
student.put("name", "foo");
System.out.println("name".hashCode());
System.out.println("foo".hashCode());

Here hashcode for name (key) - 3373707 hashcode for foo (value) - 101574

my doupt is the one that it should use to store and retrieve an object from hashtable

+4
source share
1 answer

As you can see from the following code in HashMap, it uses its own hash function:

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

hashCode, xor 16 .

, hashCode .

+4

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


All Articles