[Source code analysis] The data structure of HashMap

Before, I studied the implementation of the put() method of HashMap in the Java source code, and later I looked at the overall data structure of HashMap and recorded it.

What is HashMap

HashMap is often used to store data and can replace javabean in some frameworks. The main features are as follows:

  • key-value pair
  • not thread safe
  • The data stored in the data has nothing to do with the insertion order
  • key can be null

Among them, the class that is similar to HashMap but is thread-safe and the key cannot be null is HashTable, but it is rarely used, and ConcurrentHashMap is generally used.

internal structure

We all know that HashMap is a group of key-value pairs inside, so what form does this key-value pair exist in
internally ? A stored key-value pair is an element in an array.
Then there are several problems.

  • When I new a HashMap, I did not specify the size of this "array"

Looking at the source code, you can know that the HashMap did not set the capacity when it was just new. It was checked when the put() method was called. If the table of this HashMap, that is, the Node[] array, is null, then initialize it and go through a series of methods. Generate a Node[] array with default capacity ( DEFAULT_INITIAL_CAPACITY=16 )

  • I have been putting key-value pairs without the exception of the array subscript out of bounds.

Because there is a constant in HashMap called loading factor ( DEFAULT_LOAD_FACTOR=0.75 ), when the percentage of the current element to the capacity is greater than the dynamic factor, the array will be automatically expanded, which is automatically maintained, so there is no feeling

  • What is Node

Remember HashMap traversal?

Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
for (Entry<String, String> entry : map.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
}

It can be seen from the traversal of HashMap that the key-value pairs inside exist in the form of Entry, so what is the relationship between Node and Entry?
In fact, Entry is the internal interface Entry in the Map interface, and Node is the implementation class of Entry.

  • So how is the key-value pair put into the HashMap

We all know that placing an array requires specifying a subscript. So what is this subscript? In HashMap, it is related to the hash value of this key. The subscript obtained after the hash value goes through a certain algorithm, and then the key-value pair is placed into the array

  • What if the hash value of the key is duplicated?

When inserting, we know that when the same key inserts different values, the old value will be replaced with the new value, and the old value will be returned. However, sometimes different keys will get the same hash value, this is It's embarrassing, so how is it resolved internally.

This we can look at the member variables of the Node class

static class Node<K,V> implements Map.Entry<K,V> {
    final int hash;
    fianl K key;
    V value;
    Node<K, V> next;
    ...
}

The point is that this next is a new Node, what is this, this is a linked list! That is to say, if this happens, then a linked list will be placed in a position of Node[], which contains repeated hash values. Node.ps
: If the linked list is too long, it will be converted into a red-black tree TreeNode inside HashMap, which can improve performance


The above is a brief introduction of HashMap. There may be any problems. If you find it, remember to comment. Thank you. Any new ideas will be updated from time to time...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325819675&siteId=291194637