The difference between HashMap 1.7 and 1.8

foreword

In Java, HashMap is a very commonly used data structure. It is a key-value pair storage container, which can quickly insert, find and delete elements. However, in different Java versions, the implementation of HashMap may be different, which will also cause its performance and behavior in different environments to be different. This article will mainly discuss the differences between HashMap in Java 1.7 and 1.8, to help readers better understand the features and optimizations of HashMap in these two versions.

data structure

In Java 1.7, the underlying data structure of HashMap is an array + singly linked list. Specifically, it stores all key-value pairs in an Entry array, and each Entry contains a pointer to the next Entry, so multiple Entry exists in the form of a linked list. When the hash conflicts, the new Entry will be inserted at the end of the linked list.

In Java 1.8, the underlying data structure of HashMap is array + linked list/red-black tree. In fact, on the basis of Java 1.7, a new structure is added: red-black tree. When the length of the linked list reaches the threshold (8 by default), the linked list will be converted into a red-black tree. The advantage of this is that in the case of more searches, insertions, and deletions, the performance of the red-black tree is better than that of the linked list.

So, why are linked list and red-black tree two data structures used in Java 1.8? On the one hand, linked lists are faster for small-scale data operations; on the other hand, red-black trees perform better for large-scale data operations. Therefore, in Java 1.8, HashMap will adaptively select the appropriate data structure according to the data size.

Expansion mechanism

When HashMap has too many elements, it will expand the array to relieve the pressure. The expansion mechanism of HashMap is also different in different versions.

In Java 1.7, the expansion mechanism of HashMap is to recreate a larger array based on the original array, and then add the elements in the original array to the new array one by one. This process is time-consuming and performance-intensive.

In Java 1.8, the expansion mechanism of HashMap has changed. First, it will calculate the value of (capacity * load factor), if the current value exceeds this value, it will trigger expansion. At this point, it creates a new array twice the size of the original array and migrates the original data into the new array. This process is faster and more space-efficient than the expansion mechanism in Java 1.7.

In addition, in Java 1.8, if the number of elements in the old linked list is less than 8, and the number of elements in the entire HashMap is greater than 64, it will not be converted into a red-black tree, which can avoid excessive space consumption.

add element

When adding elements to HashMap, if there is already an element at the position, it needs to be replaced or inserted. The implementation of HashMap is also different in different versions.

In Java 1.7, the way to add elements to HashMap is relatively simple. If the element position in the array is empty, insert the element directly; if there is an element, traverse the linked list to insert.

In Java 1.8, when using a linked list, elements are added in the same way as in Java 1.7. However, when the length of the linked list reaches the threshold (8 by default), the linked list will turn into a red-black tree. The way of adding elements in a red-black tree is different from the way of inserting elements in a linked list.

Specifically, if the current node is a linked list node, it will execute the same insertion logic as Java 1.7; if the current node is a red-black tree node, it will use the red-black tree method to insert. The advantage of this is that the performance of the red-black tree is better than that of the linked list in the case of many searches, insertions, and deletions.

get element

HashMap needs to do a lookup when getting an element. The process of finding elements in HashMap is also different in different versions.

In Java 1.7, the way HashMap finds elements is to first find the corresponding array subscript through the hash value, and then search sequentially from the linked list.

In Java 1.8, the way to find an element is to first find the corresponding array subscript through the hash value. If the position is a linked list node, search in the linked list order; if the position is a red-black tree node, use the red-black tree search method. According to the experimental results, when the number of elements is not very large, the search performance of the linked list is better than that of the red-black tree, because the red-black tree needs to perform operations such as comparison and rotation, and there will be a certain performance loss.

Guess you like

Origin blog.csdn.net/weixin_58724261/article/details/131236430