How does the HashMap in Java work?

Source: Internet
Author: User
Tags array length

HashMap in Java is a key-value pair (Key-value) that stores elements. HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/collection. When the put () method is called, HashMap calculates the hash value of the key and stores the key-value pair on the appropriate index in the collection. If the key already exists, value is updated to the new value. Some of the important features of HashMap are its capacity (capacity), load factor (payload factor) and expansion limit (threshold resizing).


HashMap is a data structure for a Key-value key-value pair, Structurally speaking before jdk1.8 is implemented by the array-linked list, jdk1.8 adds a red-black tree, the default initial length of the HashMap array is that the 16,hashmap array allows only one key to be null, allowing multiple value to be null
HashMap's internal implementation, HASHMAP is implemented using the form of an array + linked list + red-black tree, where the array is a node[] array, we call him a hash bucket array, which holds the nodes of the Key-value key-value pairs. HashMap is to use a hash table to store, in the HashMap to solve the hash conflict, using the chain address method, simply is the form of the array chain list to solve, when the data is hashed, the array subscript, put the data in the corresponding table of the linked list.
and then again, the HashMap method is implemented
put method, put method of the first step, is to calculate the put element in the hash bucket array index position, get the index position need three steps, go to put element key hashcode value, high operation, modulo operation, high operation is the first step to get the value of H, Using H's high 16-bit and low 16-bit for the XOR operation, the third step in order to make the hash bucket array element distribution more evenly, take the modulo operation, the modulo operation is the value obtained by the second step and the value of the hash bucket array length-1 to take. The result is consistent with the traditional modulo operation, and the efficiency is higher than the modulus calculation .
jdk1.8 The concrete steps of the Put method, first determine whether the HashMap is empty, for empty words to expand, not empty to calculate the hash value of key I, and then see Table[i] is empty, is empty directly inserted, not NULL to determine the current position of the key and Table[i] Whether the same, the same overlay, not the same view Table[i] is a red-black tree node, if it is a red-black tree directly inserted key value pairs, if not to start traversing the list inserted, if you encounter duplicate values on the overlay, otherwise directly inserted, if the list length is greater than 8, into the red-black tree structure, After execution, see if size is greater than the threshold threshold, greater than on the expansion, or directly end
The Get method is calculated to get the hash value of the element, to the corresponding position can be taken.
expansion mechanism, the hashmap of the expansion of the main two, the first step to the array length of twice times the original, the second part of the old array of elements recalculated hash into the new array, In the jdk1.8, do not recalculate the hash, only to see the original hash value of the new one is zero or 1, if it is 1 this element in the new array position, the original array is the position of the original array length, if 0 is inserted into the original array. Expansion process the second part of a very important method is the transfer method, using the head interpolation method, the old array of elements into the new array.
3.hashmap Size Why is the power of 2 square
in the calculation of the insertion element in the hash bucket array of the third step, in order to make the element distribution more evenly, with the mold operation, but the traditional mode operation efficiency is low, and then optimized to h& (LENGTH-1), set to 2 power, is because 2 of the power of 1 after the value of each one is 1, Then, with the H value calculated in the second step, the final result is only related to the hashcode value of the key itself, which does not cause wasted space and is evenly distributed, if not a power of 2
If length is not a power of 2, for example 15. Then the Length-1 2 will turn into 1110. In the case of H for random number, and 1110 do & operation. The mantissa is always 0. Then 0001, 1001, 1101, such as the mantissa 1 position will never be entry occupied. This can cause waste, not random problems.

How does the HashMap in Java work?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.