Let’s review a wave, and analyze the underlying implementation principle of HashMap

Preface

HashMa is the most commonly used collection framework in Java. It is also a very typical data structure in the Java language. It is also a data structure that we need to master. More importantly, it is one of the must-questions for interviews in large factories.

 

Array characteristics

The storage interval is continuous, and it takes up a lot of memory, the space is complicated, and the time complexity is O(1).

Advantages: The efficiency of random reading is very high, because the array is continuous (strong random accessibility and fast search speed).

Disadvantages: The efficiency of inserting and deleting data is low. Because of inserting data, the data behind this position must be moved back in the memory, and the size is fixed and it is not easy to dynamically expand.

Linked list features

The interval is discrete, the memory is loose, the space complexity is small, and the time complexity is O(N).

Advantages: fast insertion and deletion speed, high memory utilization, no fixed size, flexible expansion.

Disadvantages: It cannot be searched randomly, and it is traversed from the first one every time (inefficient query).

Hash table characteristics

Everyone knows the advantages and disadvantages of the above arrays and linked lists. So can we use the above two together to achieve a data structure with high query efficiency and high insertion and deletion efficiency? The answer is yes, that is, the hash table can be satisfied. Next, let's review the implementation principles of put() and get() methods in HashMap together.

 

Implementation of put() and get() of HashMap

1. The realization principle of map.put(k,v)

The first step is to encapsulate k and v into a Node object (node). In the second step, its bottom layer will call K's hashCode() method to get the hash value. The third step is to convert the hash value into the subscript of the array through the hash table function/hash algorithm. If there is no element in the subscript position, add the Node to this position. If there is a linked list at the position corresponding to the subscript. At this time, it will hold k and the k of each node on the linked list for equal. If all equals methods return false, then this new node will be added to the end of the linked list. If one of equals returns true, then the value of this node will be overwritten.

2. The realization principle of map.get(k)

The first step: first call the hashCode() method of k to get the hash value, and convert it into the subscript of the array through the hash algorithm. The second step: After converting into the subscript of the array by the previous hash algorithm, quickly locate a certain position through the subscript of the array. It is important to understand that if there is nothing in this position, null is returned. If there is a singly linked list at this position, it will equals the parameter K and the K of each node on the singly linked list. If all equals methods return false, the get method returns null. If the K of one of the nodes and the parameter K are equals to return true, then the value of the node is the value we are looking for, and the get method will eventually return the value we are looking for.

3. Why are random additions and deletions and query efficiency high?

Reason: Additions and deletions are done on the linked list, and the query only needs to scan part, which is efficient.

The keys of the HashMap collection will call two methods successively, the hashCode and equals methods, both of which need to be rewritten.

 

4. Why do the elements placed in the key part of the hashMap collection need to override the equals method?

Because equals compares the memory addresses of two objects by default

Key features of HashMap collection:

5. HashMap summary

Disordered, non-repeatable Why is disordered? Because it is not necessarily linked to which singly linked list, the order of addition and removal are different. How to keep it non-repeatable? Use the equals method to ensure that the key of the HashMap collection is not repeatable. If the key is repeated, the value will be overwritten. The elements stored in the key part of the HashMap collection are actually stored in the HashSet collection, and the HashSet collection also needs to rewrite the equals and hashCode methods. The default initial capacity of the hashmap collection is 16, and the default load factor is 0.75, which means that the default load factor is when the capacity of the underlying array of the hashmap collection reaches 75%, the array starts to expand. The initial capacity of the hashmap set is a co-number of 2. In order to achieve uniform hashing and improve the access efficiency of the hashmap set,

6, pay attention to after JDK8

After JDK8, if there are more than 8 elements in the hash form to the linked list, the data structure of the singly linked list will become a red-black tree data structure. When the number of nodes on the red-black tree is less than 6, the red-black tree will be changed into a singly linked list data structure again.

 

problem:

If the hash values ​​of O1 and O2 are the same, they will be stored in the same singly linked list,

If they are different, but the converted array subscripts may be the same after the execution of the hash algorithm, a "hash collision" will be sent at this time.

7. High-frequency interview questions

What is the working principle of HashMap? What is the "deadlock" in HashMap? Can two same keys be put in HashMap? why? Can the key value in HashMap be null? principle? HashMap expansion mechanism? Thank you everyone for seeing this. If you need big data, Java tutorials, you can follow me, and send "Java, Springboot, mysql" and other related terms through private messages to get more tutorials.

Staying up late is not easy, creating is not easy. Your support and recognition is the biggest motivation for my creation. See you in the next article! If the typesetting or writing is not good enough, welcome to communicate in the comment area.

Guess you like

Origin blog.csdn.net/xiaokanfuchen86/article/details/113426501