An article to find out the internal structure of HashMap and TreeMap

一、HashMap

1, based on the Map interface to achieve the hash table.

This implementation provides all of the optional map operations, and allows the use null null values ​​and keys. (In addition to allowing the use of null and non-synchronous addition, the HashMap Hashtable class is approximately the same.) This mapping does not guarantee the order, in particular, it does not guarantee that the order constancy.

2, there are two examples HashMap affect its performance parameters: initial capacity and load factor.

Capacity is the number of buckets in the hash table, the initial capacity of the hash table only when capacity is created.
The load factor is a hash table before automatically increase its capacity can reach more than a full scale. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, it will have to be rehash operations (i.e., reconstruction of the internal data structures) of the hash table, the hash table so as to have approximately twice the number of barrels.
And the hash value according to the length of the array of buckets key keyword modulo find the location of the tub, the same key if the hash value, Hash conflict (i.e. pointing to the same bucket), as each new head node is added, and added first end of the table.
The number of buckets in the HashMap length of the array is the location of 0- n of the figure, the first entry is stored called bucket (bucket) while the bucket can store a value that is the head of the linked list of nodes, each chain a node value is added (after HashMap instance Entry Entry inner class attribute which said detail).
Can also be understood, the entry list stored in an array type. Index position in the array is a buckets index address.
From the graph we can find a hash table is an array of + chain consisting of an array of length 16, each element is stored in a linked list of the first node. Then these elements are in accordance with what rules it is stored in the array.
% Len is generally obtained by hash (key), the key is a hash value obtained modulo element of the array length. In the above example, the hash table, 12, 28 = 12% 16% 16% = 12,108 16% = 12,140 16 = 12. Therefore, 12,28,108 and 140 are stored in the array index 12 for the location.

HashMap brief summary:

1, HashMap array is a chain (linked list stored in the array) can be implemented search speed, and can quickly acquire a corresponding key value;
2, the factors have the capacity query speed and the load factor, the load factor small capacity query fast but a waste of space, whereas the opposite;
. 3, the array index value (key keyword, hashCode hash key value, the size of the array len): hashcode% len value is determined, a large load capacity is small if the index is the same factor (i.e. pointing to the same index the probability of the same bucket) little, small chain length of the query speed, whereas the same probability of large index list long slow queries.
4, as well as for HashMap subclass, they are using hash algorithm to determine the storage elements in the set position, when the HashMap initialization when the system creates a length capacity of Entry array, the array can store elements of said position is a barrel (bucket), each of which has a bucket index is specified, the system can quickly access elements according to the index stored in the tub.
5, whenever the HashMap each bucket only a storage element (Entry object). Entry Since the object can contain a reference point for a variable Entry, it may occur HashMap bucket (bucket) only one Entry, but the points to another Entry Entry Entry thus forming a chain.
6, found HashMap at the bottom will key_value to as a whole for processing (Entry object) the whole is an Entry object by the above code, when the system determines the storage in the HashMap key_value of, is not considered Entry in value, but only It is determined according to the storage location of each Entry hash key value.
important point
JDK1.8 using a Node array to store data, but this may be the Node list structure, it may be the same red-black tree structure if the key is inserted hashCode, then the key will be positioned to the same point on a grid array Node .
If the same key of the grid is not more than eight, using a linked list structure to store. If you have more than eight, then calls treeifyBin function to convert the list is red-black tree. Even if hashcode exactly the same, due to the characteristics of the red-black tree, find a specific element need only O (log n) overhead.
That is the time complexity of the operation put / get the worst only O (log n).
Note: key objects must be properly realized Compare Interface

二、TreeMap

1, red-black tree is an approximately balanced binary search tree, it can ensure that the height difference between left and right subtrees of any node that does not exceed the lower of the two in doubles. Specifically, the following conditions are met red-black tree is a binary search tree (binary search tree):
  1. Each node is either red or black.
  2. Root node must be black
  3. Red node can not be continuous (ie, the red node of children and the father can not be red).
  4. For each node, a path from any point to the null (the trailing end of the tree), and contain the same number of black nodes.
When the tree structure is changed (insertion or deletion), tend to disrupt the condition 3 or condition 4 above, must be adjusted so that the search tree again satisfy the conditions of the red-black tree.
2, the bottom TreeMap uses red-black tree to achieve, as TreeMap object into a key-value key-value pairs, generates an Entry object, the object is a red-black tree node, in fact, and this is HashMap like, an Entry object as a node, but in different ways stored in these nodes.
3, each Entry object storage will be stored according to the specification of a binary tree according to the size of the key button, the data is TreeMap in ascending order according to the key.

TreeMap Summary:

When adding a new node program, always start comparing the root of the tree, the root node as the current node is about. If the new node is greater than the current node and right node of the current node exists, the right node as the current node, if the presence of the left child node and the new node is less than the current node of the current node, places the left child node as the current node;
If you add a node is equal to the current node, then covered with a new current node, and the end of the cycle until about a child node of a node does not exist, add a new child node for node node. If the new node is greater than this node, then add it to the right child node. If the new node is smaller than this node, then add it as a left child node.

At last

I welcome everyone's attention the public Herd number [programmer], in which the article will be updated, data compilation will be on the inside.


Guess you like

Origin juejin.im/post/5d9c795c518825095e3d7461