handwriting implements a HashMap
Objective
HashMap is commonly used in Java collection, and hashmap some of the ideas, for our peacetime solutions to some of the problems in the business, in the way of thinking, based on this, this blog will analyze the HashMap bottom design ideas, and handwriting a mini version of the HashMap.
a reflection on the HashMap
HashMap underlying data structure
First, as shown in the picture, HashMap has 3 elements: hash function + array + single linked list
Second, what you need to consider for a hash function.
To be quick, for a given key, be able to quickly compute the index in the array. So what's fast enough? is obviously a bit operation.
To evenly distribute, less collisions. To put it bluntly, we hope that the hash function, so that the data evenly distributed in the array, do not want a large number of data collisions, resulting in the chain table too long. Then how to do it. It is also the use of bit operations, through the binary bits of the data to move the hash function to get the data scattered, thereby reducing the probability of collision.
What if a collision happens. The above figure has actually explained how the JDK HashMap deal with the hash conflict, which is solved by a single linked list. So besides this method, there are other ideas. For example, if there is a conflict, then write down the location of the conflict is index, and then add a fixed step, that is, index+step, find this position, see if there is still conflict, if you continue to conflict, then follow this line of thought, continue to add fixed step length. In fact, this is called linear detection to solve the hash conflict method.
by writing a mini version of HashMap to get a deep understanding
Defining interfaces
Defines an interface for exposing fast access to the outside world.
Note that an internal interface entry is defined inside the Mymap interface.
Myhashmap Interface Implementation
One of the elements of HashMap is the array, which is naturally here, we want to define the array, initialize the size of the array, and consider the valve value of the expansion.
Look at the structure of Myhashmap
What can be say about the construction method?
With a closer look, you'll find that the "façade mode" is actually used here. The 2 construction methods here actually point to the same one, but expose 2 "façade" to the outside world.
Entry
One of the elements of HashMap, a single linked list of the embodiment is here.
See how put is implemented
First, we have to consider whether or not to enlarge.
Whether the number of entry in the HashMap (array and all entry in a single linked list) reaches the threshold.
Second, if the expansion means that the new born into a entry[], not only so also have to be hashed out.
Third, according to the key to calculate the position in entry[], after positioning, if the element in entry[] is null, then you can put into it, if not empty, then have to traverse the single linked list, or update value, or form a new entry "squeeze" single linked list.
hash function provided by Myhashmap
The hash function provided by the HashMap of the JDK
I refer here to the implementation of the HASHMAP hash function of JDK, and here again: to hash evenly, we have to do binary bit operations.
Resize and rehash
As can be seen here, for HashMap, if the frequent resize/rehash operation, it will affect performance.
Resize/rehash process, that is, the array is larger, the original array of entry elements are put to the new array process, you need to pay attention to some of the state variable changes.
Get implementation
Get is very simple, only need to notice in the process of traversing a single list of the use of = or equals to judge.
Test testing, using Myhashmap for access
Run results