map
|—Map: double-column data, storing key-value pairs—similar to the function in high school: y=f(x)
|—HashMap: As the main implementation class of Map; thread-safe and efficient; stores null keys and values
|—LinkedHashMap: Ensures that when traversing elements, they can be traversed in the order they were added. Reason: Based on the original HashMap underlying structure, a pair of pointers are added to point to the previous and next elements. For frequent traversal operations, this type of execution is more efficient than HashMap
|—TreeMap: Ensure to sort according to the added key-value pairs to achieve sorted traversal. At this time, consider the natural sorting or customized sorting of keys. The bottom layer uses red and black numbers
|—Hashtable: As an ancient implementation class of Map; thread-safe, low efficiency; does not store null keys and values
|—Properties: Commonly used to process configuration files. Both key and value are of type String
The bottom layer of HashMap: array + linked list (jdk7 and before)
Array + linked list + red-black tree (jdk8)
2. Understanding of Map structure
Keys in Map: Unordered, non-repeatable, use Set to store all keys -> the class where the key is located must override equals() and hashCode() (HashMap is an example)
Value in Map: unordered, repeatable, use Collection to store all values—>the class where value is located must override equals()
A key-value pair: key-value constitutes an Entry object.
Map entries: unordered, non-repeatable, use Set to store all entries
3. The underlying implementation principle of HashMap? Take jdk7 as an example
- Hash map=new HashMap();
After instantiation, the bottom layer creates a one-dimensional array Entry[] table with a length of 16. - …may have been executed multiple times…
- map.put(key1,value1)
First, call hashCode() of the class where key1 belongs to calculate the hash value of key1. After this hash value is calculated by a certain algorithm, the storage location in the Entry array is obtained.
If the data at this location is empty, key1-value1 is added successfully. ——->Case 1
If the data at this location is not empty, (meaning there is one or more data (in the form of a linked list) at this location), compare key1 with the hash value of one or more data that already exists:
If the hash value of key1 is different from the hash value of the existing data, key1-value1 is added successfully. ——->Situation 2
If the hash value of key1 is the same as the hash value of an existing data (key2-value2), continue the comparison: call the equals(key2) method of the class where key1 is located, and compare:
If equals() returns false: key1-value1 is added successfully. ——->Situation 3
If equals() returns true: replace value2 with value1.
Supplement: Regarding case 2 and case 3: at this time key1-value1 and the original data are stored in a linked list.
In the process of continuous addition, the issue of capacity expansion will be involved. The default expansion method is to expand the capacity to 2 times the original capacity and copy the original data.
Differences in underlying implementation between jdk8 and jdk7
1.new HashMap(): The underlying array is not created with a length of 16
2. The underlying array in jdk 8 is Node[], not Entry[]
3. When the put() method is called for the first time, an array of length 16 is created at the bottom
4. The underlying structure of jdk7 is only: array + linked list. Structure in jdk8: array + linked list + red-black tree.
When the number of data elements in a linked list at a certain index position of the array is >8 and the current array length is >64,
At this time, all data at this index location is instead stored using a red-black tree.
DEFAULT_INITIAL_CAPACITY:Default capacity of HashMap: 16
DEFAULT_LOAD_FACTOR: The default load factor of HashMap: 0.75
threshold: The critical value of expansion = capacity fill factor 160.75 = 12 TREEIFY_THRESHOLD: The length of the linked list in the Bucket is greater than the default value, converted into a red-black tree: 8
MIN_TREEIFY_CAPACITY: The minimum hash table capacity when the Node in the bucket is tree-formed: 64\
4. The underlying implementation principle of LinkedHashMap (understand)