Java Collection-Supplemental HashMapJDK1.8

Source: Internet
Author: User

Prior to Java 8, HashMap and other map-based classes resolved conflicts by chaining address methods, which used a unidirectional list to store elements of the same index value. In the worst case, this approach reduces the performance of the HashMap get method from O (1) to O (n). To address the problem of hashmap performance degradation during frequent collisions, Java 8 uses a balanced tree to replace the elements of the linked list storage conflict. This means that we can increase the worst-case performance from O (n) to O (Logn).
Use the constant treeify_threshold in Java 8 to control whether or not to switch to the balance tree for storage. Currently, this constant value is 8, which means that when the index of more than 8 elements is the same, HashMap uses the tree to store them.
This change is to continue to optimize common classes. You may recall that in Java 7, in order to optimize commonly used classes, the ArrayList and HashMap are deferred loading mechanisms that do not allocate memory until an element is joined, which reduces the empty list and the memory used by the HashMap.
This dynamic feature allows HashMap to start using a linked list and replace the linked list with a balanced binary tree when the number of conflicting elements exceeds a specified value. However, this feature is not in all hash table-based classes, such as Hashtable and Weakhashmap.
Currently, only Concurrenthashmap,linkedhashmap , and HashMap Use the balance tree in situations of frequent conflict.

When will conflict arise?

The hashcode () method is called in HashMap to calculate the hashcode.
Since two different objects in Java may have the same hashcode, different keys may have the same hashcode, resulting in conflicting results.

Summarize
    • HashMap uses a linked list to store elements of the same index when dealing with conflicts.
    • Starting with Java 8, Hashmap,concurrenthashmap and Linkedhashmap will use a balance tree instead of a linked list when dealing with frequent collisions, and when the number of elements in the same hash bucket exceeds a certain value, the linked list switches to the balance tree, which will get () The performance of the method is increased from O (n) to O (Logn).
    • When you switch from a linked list to a balance tree, the order of the HashMap iterations changes. This does not cause any problems, however, because HashMap does not provide any guarantees for the sequence of iterations.
    • The Hashtable class exists from Java 1 in order to ensure that the iteration order is not changed, even if the balance tree is not used in the case of frequent collisions. This decision was made in order not to break some older Java applications that relied on the Hashtable iteration order.
    • In addition to Hashtable, Weakhashmap and Identityhashmap do not use the balance tree in frequent conflict situations.
    • Using HashMap causes conflicts because the Hashcode () method of the Key object is used, and the Equals () and Hashcode () methods do not guarantee that the hashcode of different objects are different. It is important to remember that the hashcode of the same object must be the same, but the same hashcode is not necessarily the same object.
    • In Hashtable and hashmap, conflicts arise because the Hashcode () method of different objects returns the same value.

This is how HashMap handles conflicts in Java. This method is referred to as the chain address method because the linked list is used to store elements within the same bucket. Usually the situation is hashmap,hashset,linkedhashset,linkedhashmap,concurrenthashmap,hashtable, Both Identityhashmap and Weakhashmap use this approach to deal with conflicts.

Starting with JDK 8, hashmap,linkedhashmap and Concurrenthashmap used the balance tree to replace the linked list in the case of frequent collisions in order to improve performance. Because HashSet internal use of the Hashmap,linkedhashset internal use of linkedhashmap, so their performance will also be improved.

Java Collection-Supplemental HashMapJDK1.8

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.