Deep understanding of HashMap in Java

Source: Internet
Author: User
Tags array length bitwise rehash

1. What is the structure of HashMap?

Two-dimensional structure, the first dimension is an array, the second dimension is a linked list

2. What is the process of the Get method?

First call the key's Hashcode method to get the hash value of the object, and then use the hash value of the first-dimensional array to take the length of the model, to get the subscript of the array. The element where the array subscript is located is the table header of the second-dimension list. It then iterates through the list, using the equals of key to compare with the linked list element, and the success of the match returns the value stored in the linked list element.

3. What is the time complexity of the Get method?

HashMap, if the key through the hash algorithm to obtain the array index position is all different, that is, the hash algorithm is very good, then the time complexity of the Getkey method is O (1), if the hash algorithm technology results collisions very much, if the hash is very poor, All hash algorithms result in the same index position, so that all key-value pairs are concentrated in a single bucket, either in a linked list, or in a red-black tree, with the time complexity of O (n) and O (LGN) respectively.

4, please explain the HashMap parameter loadfactor, what is its role?

Loadfactor indicates the degree of congestion of HashMap, which affects the probability of hash operations to the same array position. The default Loadfactor equals 0.75, when the hashmap contained within the element has reached the HashMap array length of 75%, the hashmap is too crowded, need to expand, hashmap in the constructor can be customized Loadfactor.

5, please explain the process of hashmap expansion?

Expansion needs to reassign a new array, the new array is twice times longer than the old array, and then traverse the entire old structure, and all the elements are re-hashed into the new structure. This rehash process is very time-consuming, especially when the hashmap is very large, it can cause the program to lag, and twice times the memory relationship will cause memory instantaneous overflow, is actually 3 times times the memory, because the old structure of memory before the end of the rehash can not be collected immediately. Then why not in the HashMap larger than the expansion of a little bit, I do not have a very satisfactory answer to this question, I only know that the hash of the modulo operation using a bitwise operation, the bitwise operation needs to limit the length of the array must be 2 exponent. In addition, Java heap memory is used in the tcmalloc of the library, their allocation unit in memory management is the unit of 2 exponent, twice times the increment of memory can help reduce memory fragmentation, reduce the burden of memory management.

This article transferred from: https://www.toutiao.com/i6504574147786441229/?group_id=6504574147786441229&group_flags=0

Deep understanding of HashMap in Java

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.