I don't need to explain the usage of HashMap. It is often used to store unordered key-value pairs. It is a widely used data structure because of its good search and insertion performance. HashMap-related methods include get and put. Common parameters include loadFactor and Capacity, which will be mentioned later. The other two similar classes are HashTable and java. util. concurrent. concurrentHashMap, the main difference between these three classes is the performance during multithreading and whether it is thread safety. To put it simply, the performance HashMap> ConcurrentHashMap> HashTable, but HashMap is non-thread-safe, while the rest are secure.
Recently, I was asked about the implementation of hashMap, so I spent some time reading it completely. If you don't talk much about it, go to the topic.
Basic Principles
Java. util. HashMap has an Entry [] table in which data is stored.
First, the entry is a node used to implement the linked list. It has a key, a value used to store the data of its own node, and next used to reference the next node. Let's look at the get method in HashMap: We can get these conclusions: get is the process of finding the corresponding entry. The entry chain is placed in the n buckets of the table array. 1. processing nullkey separately is actually placed in table [0. 2. Determine the bucket in which the entry is located based on the hash value of the key. 3. traverse the entry linked list in the bucket and return value when searching. 4. If no value is found, null is returned.
Here we need to insert an irrelevant,
LoadFactor and initCapacity Let's look at the default HashMap constructor: static final int MAXIMUM_CAPACITY = 1 <30; static final float DEFAULT_LOAD_FACTOR = 0.75f; int threshold; final float loadFactor; you can see clearly, by default, we construct a map with an initial capacity of 16 and a loading Factor of 0.75f. When will the first expansion take place? 16*0.75 = 12Interesting details Addressing is through index & (table. length-1) is actually a modulo. if length is a multiple of 2, table. length-1 is always 111111... structure, and operation can easily obtain the mod value. Avoid the worst case. Int hash = hash (key. hashcode (); ---- refer to the get method above to see what the hash function does. From the annotations, we can see that this function ensures that the hashcode does not have the worst case (that is, most kv values are distributed to the same bucket) by means of displacement and exclusive ). There is a maximum of 8 conflict in the default load factor. In fact, it is to make the distribution more evenly. The following is a reference to StackOverflow's foreign explanation:
About resizingAs mentioned above, when the capacity reaches the threshold, HashMap will resize. What did you do during resizing? As you can see, what you do includes: 1. apply for a new space to store table 2. transfer (move the old data to the new table), and point the table to the new table (the old table is automatically garbage collected later) 3. adjust the new threshold value so that the next resize can be activated in the put method. If you study the put method, you can see: (size ++> = resize (2 * table. length );View Code