This paper is based on the analysis of HashMap in Java 8. It mainly introduces the use of member variables and class variables in HashMap, and analyzes the data structure of HashMap.
There are multiple member variables and class variables in the HashMap. It is helpful for us to have a better understanding of their purpose. Here are their introduction:
/** * Default initial capacity, must be to the power of 2 */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // It's known as 16 /** * Maximum capacity */ static final int MAXIMUM_CAPACITY = 1 << 30; /** * Default load factor */ static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * The threshold value of converting a linked list to a red black tree. When the number of linked list nodes is greater than or equal to the threshold value of - 1, it will be converted to a red black tree */ static final int TREEIFY_THRESHOLD = 8; /** * The threshold value of converting a red black tree to a linked list. When the node of a red black tree is less than the threshold value, it is converted to a linked list */ static final int UNTREEIFY_THRESHOLD = 6; /** * The threshold value to allow conversion of linked list to red black tree. Only the hash size is greater than or equal to this value can the red black tree be converted */ static final int MIN_TREEIFY_CAPACITY = 64; /** * HashMap An array in which data is stored, also known as a hash table. * It is recommended to keep the length to the power of 2 */ transient Node<K,V> table; /** * Cache the value of the entrySet() method */ transient Set<Map.Entry<K,V>> entrySet; /** * Map Number of middle key value pairs */ transient int size; /** * HashMap The number of times the data structure has been changed, generally refers to the change of hash table length, the increase or decrease of Node chain list * This parameter is used for quick failure mechanism */ transient int modCount; /** * The threshold value of the next trigger resizing (resize() method), which is usually the capacity multiplied by the load factor */ int threshold; /** * Load factor of hash table, used to calculate the threshold of capacity expansion */ final float loadFactor;
HashMap uses the zipper method to solve the hash conflict problem in the hash table, so the bottom layer of HashMap uses the array table composed of Node as the element to store key value pairs, and each Node is a key value pair object. Table is called a hash table.
Table corresponds to hash table, because when storing or reading key value pairs, hash%table.length operation will be performed on key to hit hash table, and then Node linked list corresponding to hit index will be operated (or key and hash will be compared).
The above is the implementation of HashMap before Java 8, and Java 8 has been optimized: when the number of linked list nodes exceeds the threshold value of treeify thureshold (8), the linked list will be converted into a red black tree.
It's hard to understand if you just use words, so here's a picture: