One step to learn the implementation principle of J2SE-HashMap,

Source: Internet
Author: User

One step to learn the implementation principle of J2SE-HashMap,
HashMap Data Structure

The data structure of HashMap is implemented through the array and linked list. Arrays are the subject of HashMap, and linked lists are used to solve the Hash collision problem.

  

HashMap Of Put Method

1. When put, first judge whether the key value is null. If it is null, the null value is the location where the key is placed.

2. If the key value is not null, hash is performed on the key value. If the obtained value does not have other entries in the array position, the value is placed in the corresponding position of the array.

3. If a value already exists in the array position after hash, it indicates that a collision has occurred and will be compared with the key values of all entries in the position.

4. If the key value of the comparison is the same element, the old value is replaced with the new value, and the old value is returned.

5. If the key value is different, it means that the element is not the same, and the element is placed in the first position of the linked list.

Get method of HashMap

1. When getting, first judge whether the key value is null. If it is null, take the value of the position where the key is null.

2. If the key is not null, The hashCode of the key is calculated first, and then the corresponding position value in the array is taken.

3. Compare hashCode and equals with the key at the corresponding position of the array. If the value is equal, the value of this Entry is obtained.

Resize of HashMap

When there are more and more elements in HashMap, the probability of hash conflicts increases, because the length of the array is fixed. Therefore, to improve query efficiency, we need to resize the HashMap array. The HashMap expansion will recalculate the data in the original array and store it in the array. Therefore, this operation consumes a lot of performance.

When Will HashMap be resized? When the number of elements in the HashMap exceeds the array size * loadFactor, the array is expanded. The default loadFactor value is 0.75. When this condition is met, the array size will be resized to double. Then recalculate the position of each element in the array. Therefore, if we can predict the number of elements in HashMap, the number of Preset elements can effectively improve the performance of HashMap.

Fail-Fast mechanism of HashMap

HashMap is thread-unsafe. Therefore, if another thread modifies the map during the iterator iteration, ConcurrentModificationException will be thrown, which is the fail-fast mechanism.

There is a modCount field in HashMap, which records the modification times of HashMap. This value is added every time you modify the content of HashMap. During the iterator initialization process, the iterator records this value.

During the iteration process of the iterator, the value of modCount is compared to the value saved by the iterator before each data fetch. If the value is not equal, it indicates that HashMap is modified, and an exception is thrown.

 

Related Article

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.