Research on hash storage mechanism by analyzing JDK source code

Source: Internet
Author: User
Tags hash

Analysis of its Hash storage mechanism through HASHMAP and HashSet source code

Collections and references

Just like an array of reference types, when we put a Java object into an array, we are not really putting the Java object into an array, but putting the reference of an object into an array, each of which is a reference variable.

In fact, there are many similarities between HashSet and HashMap, for HashSet, the system uses the Hash algorithm to determine the storage location of the set elements, so as to ensure that the collection elements can be saved and fetched quickly. For HashMap, System key-value as a whole Processing, the system always calculates the Key-value storage location according to the Hash algorithm, which can guarantee the key-value pair of the Map to be saved and fetched quickly.

Before introducing the collection store, it is important to point out that while the collection is known to store Java objects, it does not actually put Java objects into the set collection, but rather to keep references to those objects in the Set collection. That is, a Java collection is actually a collection of multiple reference variables that point to the actual Java object.

Storage implementation of HASHMAP

When the program tries to put multiple key-value into the HASHMAP, take the following snippet as an example:

HashMap<String , Double> map = new  HashMap<String , Double>();
map.put("语文" , 80.0);
map.put("数学" , 89.0);
map.put("英语" , 78.2);

HashMap uses a so-called "Hash algorithm" to determine where each element is stored.

When the program executes Map.put ("language", 80.0); , the system will invoke the Hashcode () method of "language" to get its hashcode value-each Java object has a hashcode () method that can obtain its hashcode value. After the hashcode value of the object is obtained, the system will determine the storage location of the element based on the hashcode value.

We can see the source code for the put (K key, V value) method of the HashMap class:

public V put(K key, V value)
{
// 如果 key 为 null,调用 putForNullKey 方法进行处理
if (key == null)
return putForNullKey(value);
// 根据 key 的 keyCode 计算 Hash 值
int hash = hash(key.hashCode());
// 搜索指定 hash 值在对应 table 中的索引 
int i = indexFor(hash, table.length);
// 如果 i 索引处的 Entry 不为 null,通过循环不断遍历 e 元 素的下一个元素
for (Entry<K,V> e = table[i]; e != null; e =  e.next)
{
Object k;
// 找到指定 key 与需要放入的 key 相等(hash 值相同
// 通过 equals 比较放回 true)
if (e.hash == hash && ((k = e.key) == key
|| key.equals(k)))
{
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
// 如果 i 索引处的 Entry 为 null,表明此处还没有 Entry
modCount++;
// 将 key、value 添加到 i 索引处
addEntry(hash, key, value, i);
return null;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.