A brief talk about Java Knowledge Point--HASHMAP

Source: Internet
Author: User

  A) HashMap basic brief

HashMap is mainly based on arrays and linked lists , the elements stored in the array is a one-way list, so we can interpret HashMap as a one-way linked list array.

In the HashMap source code we can see some of the key properties, such as: transient entry[] table; This is an array of entities used to store the elements, where the type of the array is entry, which is consistent with our definition of hashmap above.

1 transient entry[] table;

  

  II) get operation of HashMap

HashMap data storage will be calculated by the key hashcode hash value, and then according to the hash value to calculate the array index should be stored (in which array, the corresponding index is how many), is actually stored in the array in the one-way list, and in the head of the chain. after the completion of the data put, it is necessary to determine whether the size (number of storage elements) has reached the critical value (threshold), if the threshold is reached to expand, the size after expansion is twice times the current size. expansion is called resize (), because the data in the original array at the time of expansion must recalculate its position in the new array and put in, it is very performance-intensive operation, if we can predict the number of elements in HashMap, Then the number of preset elements can effectively improve the performance of HashMap.

Add: But in fact hashmap this expansion design has an implicit flaw, because it is put after the expansion if there is no put operation then the expansion is invalid expansion, not only wasted capacity also consumes the performance.

  III) put operation of HashMap

When hashmap the Get data operation, it calculates the hash value by the key's Hashcode, calculates the index of the stored array according to the hash value, and then finds the required element in the linked list through the Equals method of key, and finds the element in the list through the loop.

  

  IV) Limitations of HASHMAP

  because the HashMap is non-thread-safe, the linked list can easily form a closed link if the resize () is thrown at the same time, resulting in a dead loop on get, and a CPU occupancy rate of 100%. Therefore, you should use CONCURRENTHASHMAP for multithreaded programming.

  (v) Other instructions

The capacity of an array must be 2 of the whole number of powers: is to reduce the probability of collisions of different hash values, so that the elements in the array evenly hash, the relative query without traversing a position on the linked list to improve efficiency.

Thanks for the inspiration from the following blog posts:

1) http://www.cnblogs.com/ITtangtang/p/3948406.html

A brief talk about Java Knowledge Point--HASHMAP

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.