The working principle of hashmap is a common Java face test in recent years. Almost every Java programmer knows HashMap, knows where to use HashMap, knows the difference between Hashtable and HashMap, so why is this question so special? Because the depth of the study is very deep. This problem often occurs in senior or intermediate interviews. Investment banks prefer to ask this question and even ask you to implement HASHMAP to examine your programming skills. The introduction of Concurrenthashmap and other synchronized sets makes the problem more complicated. Let us begin the journey of discovery!
Let's start with some simple questions.
"Have you ever used HashMap?" "What is HashMap?" Why do you use it? ”
Almost everyone answers "yes" and then answers some of HashMap's features, such as HashMap can accept null key values and values, while Hashtable cannot; HashMap is synchronized; HashMap is fast; and HashMap stores the key-value pairs and so on. This shows that you have used hashmap and are quite familiar with it. But the interviewer came to a nosedive, starting from the moment to ask some tricky questions about HashMap's more basic details. The interviewer may ask the following questions:
"Do you know how HashMap works?" he said. "Do you know how the HashMap get () method works?" ”
You might say, "I'm not looking at the standard Java API, you can look at Java source code or open JDK." "I can use Google to find the answer." ”
But some interviewers may be able to give an answer, "HashMap is based on the principle of hashing, we use put (key, value) to store objects into HashMap, using get (key) from HashMap to obtain objects." When we pass a key and value to the put () method, we first invoke the Hashcode () method on the key, and the returned hashcode is used to locate the bucket location to store the entry object. "The key point here is to point out that HashMap is the key object and value object stored in bucket as map.entry." This helps to understand the logic of acquiring objects. If you are not aware of this, or mistakenly think that only the value is stored in the bucket, you will not be able to answer how to get the object's logic from the HashMap. The answer is quite right, and it shows that the interviewer does know how hashing and hashmap work. But this is just the beginning of the story, when the interviewer joins some Java programmers every day to encounter the actual scene, the wrong answer is frequent. The next question may be about collision detection (collision detection) and collision resolution in HashMap:
"What happens when two objects have the same hashcode?" " from here on, the real confusion begins, and some interviewers answer that because the hashcode are the same, two objects are equal, HashMap will throw exceptions, or they will not be stored." The interviewer may then remind them that there are equals () and Hashcode () two methods and tell them that the two objects, even if they are hashcode, may not be equal. Some interviewers may give up, while others can continue to advance, and they answer "because the hashcode are the same, so they have the same bucket position and the ' collision ' occurs." Because HashMap uses a linked list to store objects, this entry (a Map.entry object that contains key-value pairs) is stored in a linked list. "The answer is very reasonable, although there are many ways to deal with collisions, this method is the simplest, it is the hashmap approach." But the story is not over yet, the interviewer will continue to ask:
"If two keys have the same hashcode, how do you get the value object?" The interviewer will answer: when we call the Get () method, HashMap uses the hashcode of the key object to find the bucket location and gets the value object. The interviewer reminded him that if two value objects were stored in the same bucket, he would answer: the list will be traversed until the value object is found. The interviewer will ask, because you don't have a value object to compare, how are you determined to find the value object? Unless the interviewer is not able to answer the question until the hashmap is stored in the list as a key-value pair.
Some of those who remember this important point of knowledge will say that after finding the bucket location, they will call the Keys.equals () method to find the correct node in the linked list and finally find the value object to find. The perfect answer!
In many cases, interviewers will make mistakes in this process because they confuse the hashcode () and Equals () methods. Because the hashcode () is repeated before this, the Equals () method appears only when the value object is fetched. Some good developers will point out that using immutable, declared final objects, and using the appropriate equals () and Hashcode () methods, will reduce collisions and increase efficiency. Immutability makes it possible to cache the hashcode of different keys, which increases the speed of the entire fetch object, and it is a good choice to use a wrapper class like String,interger as a key.
If you think it's over, you'll be surprised when you hear the following question. "What if the size of the hashmap exceeds the capacity defined by the load factor (load factor)?" "Unless you really know how HashMap works, you won't be answering the question," he said. The default load factor size is 0.75, that is, when a map fills up 75% bucket, as with other collection classes (such as ArrayList, etc.), it will create a twice-fold bucket array of the original HashMap size to resize the map. And put the original object into the new bucket array. This process is called rehashing because it calls the hash method to find the new bucket location.
If you can answer this question, here's the question: "Do you understand what's wrong with resizing hashmap?" "You may not be able to answer, and then the interviewer will remind you that when multithreading is possible, competitive conditions may arise (race condition).
There is a real competition when resizing hashmap, because if two threads find that the hashmap needs to be resized, they will try resizing at the same time. In the sizing process, the order of the elements stored in the linked list is reversed, because when moved to the new bucket position, HashMap does not place the element at the end of the list, but on the head, in order to avoid trailing traversal (tail traversing). If the condition competition happens, then the cycle is dead. At this time, you can ask the interviewer, why so strange, in a multi-threaded environment to use HashMap it? :)
Enthusiastic readers have contributed more to the question of HashMap:
1. Why string, interger such a wrapper class is suitable as a key? string, Interger such a wrapper class as a HashMap key is a good fit, and string is most commonly used. Because string is immutable and final, the Equals () and Hashcode () methods have been overridden. Other wrapper classes also have this feature. Immutability is necessary, because in order to compute hashcode (), you should prevent the key value from changing, if the key value in the time and get back to the different hashcode, then you can not find the object you want from the HashMap. Immutability has other advantages, such as thread safety. If you can guarantee that hashcode is unchanged by simply declaring a field as final, then do so. Because the Equals () and Hashcode () methods are used to get the object, it is important that the key object rewrite the two methods correctly. If two unequal objects return different hashcode, the chances of collisions will be smaller, which can improve the performance of HashMap.
2. Can we use a custom object as a key? This is an extension of the previous question. Of course you might use any object as a key, as long as it follows the rules of the Equals () and Hashcode () method, and will not change after the object is inserted into the map. If this custom object is immutable, it already satisfies the condition of being a key because it cannot be changed after it is created.
3. Can we use Cocurrenthashmap instead of Hashtable? This is another very popular face test, because concurrenthashmap more and more people use. We know that Hashtable is synchronized, but Concurrenthashmap synchronization is better because it locks only part of the map based on the sync level. Concurrenthashmap of course can replace Hashtable, but Hashtable provides stronger thread security. Look at this blog to see the difference between Hashtable and Concurrenthashmap.
I personally like this problem, because the depth and breadth of the problem, also not directly related to different concepts. Let's take a look at these questions and design the knowledge points:
- The concept of hashing
- The method of solving collision in HashMap
- The application of Equals () and hashcode () and their importance in HashMap
- The benefits of immutable objects
- HashMap Multi-Threading condition competition
- Resize the HashMap
Summarize
The working principle of HashMap
HashMap based on the hashing principle, we store and retrieve objects using the put () and get () methods. When we pass the key-value pair to the put () method, it calls the Hashcode () method of the Key object to compute the hashcode and then finds the bucket position to store the value object. When the object is fetched, the correct key-value pair is found by the Equals () method of the Key object, and then the value object is returned. HashMap uses a linked list to solve the collision problem, and when a collision occurs, the object will be stored in the next node of the list. HashMap stores key value pairs in each linked list node.
What happens when the hashcode of two different key objects is the same? They are stored in a linked list in the same bucket location. The Equals () method of the Key object is used to find the key value pair.
Because of the great benefits of HashMap, I used HashMap as a cache in E-commerce applications. Because of the very many uses of Java in the financial field, and for the sake of performance, we often use HashMap and concurrenthashmap. You can view more articles about HashMap:
The difference between HashMap and Hashtable
The difference between HashMap and HashSet
Original link: javarevisited translation: importnew.com-Tangxiaojuan
Translation Links: http://www.importnew.com/7099.html