Thinking of HashMap and recommendation for handwritten implementation

foreword

HashMap is a commonly used collection in Java, and some ideas of HashMap are helpful for us to solve some business problems. Based on this, this blog will analyze the underlying design ideas of HashMap and write a mini version of HashMap. !

Thoughts on HashMap

Thinking of HashMap and recommendation for handwritten implementation

First, as shown in the figure, HashMap has 3 elements: hash function + array + singly linked list

Second, for the hash function, what needs to be considered?

To be fast, for a given Key, to be able to quickly calculate the index in the array. So what operation is fast enough? Obviously bit operations!

To be evenly distributed, to have fewer collisions. To put it bluntly, we hope that the data can be evenly distributed in the array through the hash function, and we do not want a large amount of data to collide, resulting in an excessively long linked list. So how to do it? It also uses bit operation to hash the data obtained by the hash function by moving the binary bits of the data, thereby reducing the probability of collision.

What if there is a collision? The above figure has actually explained how JDK's HashMap handles hash conflicts, which are resolved through a singly linked list. So besides this method, is there any other way? For example, if there is a conflict, then write down the position of the conflict as index, and then add a fixed step size, that is, index+step, find this position, and see if there is still a conflict. If the conflict continues, then follow this idea and continue Plus a fixed step size. In fact, this is the so-called linear detection method to solve the Hash conflict!

Deep understanding by writing a mini version of HashMap

define interface

Thinking of HashMap and recommendation for handwritten implementation

Define an interface that exposes fast-access methods to the outside world.

Note that the MyMap interface defines an internal interface Entry.

interface implementation

Thinking of HashMap and recommendation for handwritten implementation

One of the elements of HashMap is the array. Naturally, here, we need to define the array, the initial size of the array, and the threshold for expansion.

Look at the construction of MyHashMap

Thinking of HashMap and recommendation for handwritten implementation

What is there to say about the construction method?

If you look closely, you will find that the "facade mode" is actually used here. The two construction methods here actually point to the same one, but two "facades" are exposed to the outside world!

Entry

Thinking of HashMap and recommendation for handwritten implementation

One of the elements of HashMap, the embodiment of the singly linked list is here!

See how put is implemented

Thinking of HashMap and recommendation for handwritten implementation

First, consider whether to expand the capacity?

Does the number of Entry in HashMap (array and all Entry in singly linked list) reach the threshold?

Second, if the capacity is expanded, it means that a new Entry[] is generated, and not only that, it has to be re-hashed.

Third, calculate the position in Entry[] according to the Key. After positioning, if the element in Entry[] is null, you can put it in it. If it is not empty, you have to traverse the singly linked list or update the value. Either form a new Entry "squeeze" singly linked list!

hash function

Thinking of HashMap and recommendation for handwritten implementation

Thinking of HashMap and recommendation for handwritten implementation

I refer to the implementation of the hash function of JDK's HashMap here, and here it is again explained: in order to hash evenly, you have to perform binary bit operations!

resize和rehash

Thinking of HashMap and recommendation for handwritten implementation

It can be seen here that for HashMap, if the resize/rehash operation is performed frequently, it will affect the performance.

The process of resize/rehash is that the array becomes larger, and the entry elements in the original array are put into the new array one by one. It is necessary to pay attention to the changes of some state variables.

get implementation

Thinking of HashMap and recommendation for handwritten implementation

get is very simple, just pay attention to use == or equals to judge in the process of traversing the singly linked list.

Test test

Thinking of HashMap and recommendation for handwritten implementation

operation result

Thinking of HashMap and recommendation for handwritten implementation

If you want to learn Java performance optimization, engineering, high performance and distributed, simple language. Friends of microservices, Spring, MyBatis, and Netty source code analysis can add 454377428 to the group. There are Ali Daniel's live broadcast technology and Java large-scale Internet technology videos to share with you for free.

OK, a mini version of HashMap is written, have you learned it?

Guess you like

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