简体   繁体   English

哪个哈希码哈希映射实现用于值检索

[英]Which hash code hash map implementation uses for value retrieval

I know that hashmap actually uses hashcode to store and retrive object from hashtable but my doupt is which hashcode it uses.我知道 hashmap 实际上使用哈希码来存储和检索哈希表中的对象,但我怀疑它使用的是哪个哈希码。 map actually contains hashcode for key and hashcode for its value. map 实际上包含键的哈希码和值的哈希码。 let's consider this way让我们这样考虑

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

In here hashcode for name(key) is 3373707 hashcode for foo(value) is 101574在这里,name(key) 的 hashcode 是 3373707,foo(value) 的 hashcode 是 101574

my doupt is which one it should use to store and retrive object我怀疑它应该使用哪一个来存储和检索对象

As you can see from the following code in HashMap , it uses its own hash function:HashMap的以下代码可以看出,它使用自己的哈希函数:

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);
}

It uses the object's hashCode , but xor s it and arithmetically shifts it to the right 16 times.它使用对象的hashCode ,但对它进行xor或并在算术上将其向右移动 16 次。

To answer your question specifically, it uses the hashCode of the key and not the value.为了具体回答您的问题,它使用键的hashCode不是值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM