Summary of hashmap from the Internet

Source: Internet
Author: User

HashMap is undoubtedly the first practical tool for Java programmers, because it is "omnipotent" in terms of data storage, and the efficiency and performance are consistent with our goal. This is also because this HashMap is used too much in the application, so there are various analyses on the Internet for HashMap. Haha, I have also read the source code! I have understood its specific implementation, so I would like to summarize some of the various HashMap resolutions on the network.

Hash Algorithm in HashMap
HashMap uses the hash list, and the problem to be concerned in the hash list is how to minimize the conflict of hash values. there are usually two methods: the linked list method and the open address method. (Well! The data structure will be explained in more detail)
[Linked list method] organizes objects with the same hash value into a linked list and places it in the slot corresponding to the hash value;
[Open address method] uses an exploration algorithm to find the next slot that can be used when a slot is occupied.
[Load factor and capacity]: in JDK, the actual capacity of a HashMap is [factor] * [capacity ],
The default value of JDK is 16*0.75 = 12;
[Load factor], load factor a = actual number of elements in the hash table (n)/Capacity of the hash table (m)
Here, the load factor is used to measure the space usage of a hash. The larger the load factor, the higher the loading level of the hash list, and the less the load factor.
In JDK, HashMap uses the linked list method, while the linked list is a one-way linked list. During the deletion process, you must maintain the previous node.
[SourceCode]
For (Entry E = table [indexFor (hash, table. length)];
E! = Null;
E = e. next ){
Object k;
If (e. hash = hash &&
(K = e. key) = key | (key! = Null & key. equals (k ))))
Return e;
}
HashMap data structure: a combination of arrays and linked lists (called "linked list hash ). As shown in:

From this figure, we can get some useful information. The efficiency of the get method in hashmap is high, because in hashmap, data is stored in arrays, thus, the inherent advantage of data is used-you can directly locate the position of an element through an index. Of course, in the implementation of hashmap, this process is not directly used as usual. Let's look at the get method implementation in hashmap.
Public v get (Object key ){
If (Key = NULL)
Return getfornullkey ();
Int hash = hash (key. hashcode ());
For (Entry E = table [indexFor (hash, table. length)];
E! = Null;
E = e. next ){
Object k;
If (e. hash = hash & (k = e. key) = key | key. equals (k )))
Return e. value;
}
Return null;
}
In the method body, the hash value is used to locate the storage location of elements.
The linked list on each element is stored. When an element is inserted using the put method, the position (subscript) of the element on the array is first obtained based on the hash value of the key ), then we can place this element in the corresponding position. If other elements exist in this position, the elements will be stored in the form of a linked list at the same position. The new one is placed in the chain header, and the first one is placed at the end of the chain. HashMap uses a so-called "Hash algorithm" to determine the storage location of each element.

Implementation of resize in hashmap
When there are more and more elements in hashmap, the chance of collision will become higher and higher (because the length of the array is fixed). To improve the query efficiency, we need to expand the array of hashmap. In this process, the key point of the most performance consumption occurs: the data in the original array must be recalculated in the position of the new array and put in.
To solve this problem, we need to predict the possible size of hashmap during the development process, and initialize it to an integer power that is close to 2! However, this is not the perfect solution. Look at this guy:
For example, we have 1000 new HashMap (1000) elements, but in theory new HashMap (1024) is more suitable. However, even 1000, hashmap automatically sets it to 1024. But new HashMap (1024) is not more suitable, because 0.75*1000 1000, we must make new HashMap (2048) the most suitable, both considering the & problem, the resize problem is also avoided.

Effective use of HashMap
If you want to use HashMap effectively, you must overwrite it in its HashCode ()!
Overwrite the hashCode method so that the hashcode of objects with the same content is the same (so that the location of an element can be quickly located)
Overwrite the equals method to make the result meaningful when determining whether two keys are equal in HashMap.
When rewriting the equals method, the following three requirements must be met:
(1) Self-inverse: that is, a. equals (a) must be true.
(2) symmetry: If a. equals (B) = true, B. equals (a) must also be true.
(3) transmission: that is, if a. equals (B) = true and B. equals (c) = true, a. equals (c) must also be true.

Two rewrite suggestions should be kept in mind:
The "Not one principle" does not need to generate a unique hashcode for each different object. As long as your HashCode method enables get () to get the content put in put (), you can.
"Decentralization principle": The hashcode generation algorithm tries its best to make the hashcode values more scattered. Not many hashcodes are concentrated in one range, which is conducive to improving the performance of HashMap.

[The Source of the original article for reference. Thank you very much for your excellent work]
Http://www.javaeye.com/topic/368087
Http://www.javaeye.com/topic/539465

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.