In the Java container collection, hashMap usage is quite high. How to store (put()) and fetch (get()) hashMap is not clear to many people.

Here are some answers to the questions

** If an interviewer asked you how hashMap stores data, what would you say? 支那

  1. I think everyone knows that a Hashmap stores data in key-value pairs. Some people might say something like this: When we put(key, value), we store key as key, value as value, and if the key is the same, the new value overwrites the old value.

  2. And some people might say: Hashmap stores data based on hashing. When we call put(key,value), we call key.hashcode() on the key, and use the hashcode returned by the method to locate the bucket to store the Entry. (Entry objects contain keys and values). Figure below :(collision is not considered here)

Obviously the former and the latter answer, the latter answer is relatively good point. But this may only be the beginning of the story.


** What if two key objects have the same Hashcode? 支那

  1. For those unfamiliar with hashcode() and equals(), they might simply say that since hashcode is the same, the two objects are the same object, and the new value overwrites the old value. If you answer that, you know what happens. (Of course, the interview may remind you or ask you something else directly).

  2. Some will reply that since hashCodes are the same, their corresponding buckets are obviously the same, which leads to what is known as a collision (the underlying storage structure of a HashMap is an array + linked list). Each bucket index corresponds to a linked list. At this point, the system will find the corresponding linked list and add this Entry object to the end of the list.

  1. After finding the corresponding bucket based on hashCode, we also check the linked list one by one to see if there are any identical key objects in the list, using equals. If yes, replace the old value with the new value. If not, add the new Entry to the end of the list.

  • This concludes the discussion of the put principle of hashMap. Here’s how to get a key
  • In fact, the get principle is similar to the PUT principle, a reverse process.
  1. When we call get(key), we call the key’s hashcode method to get the Hashcode.
  2. Get the corresponding bucket according to hashCode.
  3. Since a bucket can have multiple entries in the linked list, the key’s equals method is called to find the corresponding entries
  4. Finally return the value (this sentence seems nonsense…. But I want to say it anyway).

Continue to increase knowledge……

Just like any other container, when we simply new a HashMap without specifying the size

The system will automatically initialize us with a number. What if we are storing data in a way that exceeds the capacity defined by the load factor?

  • Here’s an explanation for the load factor: The load factor (let’s say size N) is when a HashMap fills up with n times more buckets.

  • When a map is 75% full (the default load factor is 0.75), it expands, creates an array of buckets twice the size of the principle, and stores the principle data into the new array.


As we all know, when a Map is expanding a new array and moving data, it consumes time and memory. If we can predict the approximate size of the data stored, we can specify the size when creating a new HashMap to reduce the consumption caused by the expansion.

  • There may be some questions here, such as why the default load factor size is 0.75 (see some people talking about this). For this I think may be measured through a large number of data (have not gone to Baidu to see others’ answers, only represent personal views, welcome your answers)
  • Here is an explanation of the following load factors (for those of you who don’t know what load factors are for)
  1. The larger the load factor, the more elements there will be when the array is to be filled. The more elements there are, the more collisions there will be, the more elements there will be in a linked list and the slower the query will be. However, at this time the utilization of space is higher —- space for time
  2. The smaller the load, the fewer elements there will be when the array fills up, the fewer collisions there will be, the fewer elements there will be in a linked list, and the faster the query will be. However, the utilization of space is low —– time for space.

  • I’ll leave you there for now if you have any questions. Welcome to put forward

  • If there’s something wrong, you’re welcome to point it out, right

  • If you feel that you have gained something, then,, click a like bai, also hope to pay attention to my public number (DI201805) le, I will try my best to sum up the knowledge learned and some of their own feelings, views.