Tear JDK1.8 HashMap source code (on)

Tear JDK1.8 HashMap source code (on)

This article is a foreshadowing of the HashMap source code and does not involve the source code. Source
code articles: Hand tearing the JDK1.8 HashMap source code (below)

Getting Started

what is hash

Core theory: Hash is also called hash, hash, and the corresponding English is Hash. The basic principle is to convert 任意长度the input into the output through the Hash algorithm 固定长度.

The mapping rule is the corresponding Hash algorithm , and the original data is mapped to the 二进制串hash value.

Features of Hash :

  1. 反向推导The original data cannot be obtained from the hash value

  2. The input data 微小变化will get completely different hash values, and the same data will get the same value

  3. The execution efficiency of the hash algorithm is important 高效, and the hash value can be quickly calculated for long texts.

  4. The collision probability of hash algorithm is small

Because the principle of hashing is to 输入空间map the value into hash空间the inner, and the space of the hash value is much smaller than the space of the input.

According to 抽屉原理, there must be cases where different inputs are mapped to the same output.

Drawer principle: There are ten apples on the table. Put these ten apples into nine drawers. No matter how you put them, we find that there will be at least one drawer with no less than two apples in it. This phenomenon is what we all know. Said "drawer principle".

HashMap principle explanation

1. HashMap inheritance system

// HashMap继承AbstractMap
public class HashMap<K,V> extends AbstractMap<K,V>
// AbstractMap实现Map
public abstract class AbstractMap<K,V> implements Map<K,V>

image-20220416165009633

2. Node data structure analysis

// put到Map里面的数据都会被封装为一个Node<K,V>存放到散列表当中
static class Node<K,V> implements Map.Entry<K,V> {
    
    
        final int hash; // K.hash()经过一次扰动
        final K key;
        V value;
        Node<K,V> next; // hash碰撞 链表的next指针

3. Introduction to the underlying storage structure

jdk1.8Before, it consisted of array + linked list.

jdk1.8At the beginning, it consists of array + linked list + red-black tree .

image-20220416165918907

4. Analysis of put data principle

image-20220416171208553

5. What is Hash collision

After routing, the calculated subscript is the indexsame, that is, it appears Hash碰撞.

6. What is chaining

Because of Hash碰撞, thus forming 链表.

image-20220416172639239

7. Why did jdk8 introduce red-black trees?

The chaining is very serious, the linked list is too long, and the query time complexity is from O ( 1 ) → O ( N ) O(1) → O(N)O(1)O ( N ) , so the introduction is红黑树solved.

8. HashMap expansion principle

空间换时间

  • 负载因子 0.75

What does load factor mean? When we want to store 10,000 data, each hash bucket will store hundreds of data, then the length of the linked list of each hash bucket is very long, and its performance will become very low, even if it will be optimized to red Black tree; the meaning of the load factor of 0.75 is that when 75% of all our buckets have stored data, the number of buckets will be expanded, and the buckets will become more, and its expansion size is 2 of the original length. times, the default is 16, and the expansion from 16 to 32, of course, the remainder operation should also become %32.

The official test load factor of 0.75 is the best value. If the load factor is 0.5, then your hash bucket can only store half of it forever, but it takes up a lot of space and has high efficiency; if it is 0.99, then this hash bucket Very little space is wasted, but finding data will be slower.

The narrative in the API documentation:

image-20220410150927812

Article reference: Xiao Liu speaks source code video at station b

Guess you like

Origin blog.csdn.net/weixin_53407527/article/details/124230486