Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the HashMap of Java8

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use Java8's HashMap. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Java8HashMap

Java8 has made some changes to HashMap, but the biggest difference is that it uses a red-black tree, so it consists of an array + a linked list + a red-black tree.

According to the introduction of Java7HashMap, we know that when searching, we can quickly locate the specific subscript of the array according to the hash value, but after that, we need to compare one by one along the linked list to find what we need, and the time complexity depends on the length of the linked list, which is O (n).

In order to reduce the overhead of this part, in Java8, when there are more than 8 elements in the linked list, the linked list will be converted into a red-black tree, and the time complexity can be reduced to O (logN) when searching in these locations.

Next, let's introduce it with the code. I feel that the readability of the source code of Java8 is poor, but it is more concise.

Entry is used in Java7 to represent the data nodes in each HashMap, and Node is used in Java8. There is basically no difference between the four attributes key,value,hash and next. However, Node can only be used in the case of linked lists, and TreeNode is required in the case of red-black trees.

We determine whether it is a linked list or a red-black tree according to whether the first node data type in the array element is Node or TreeNode.

Slightly different from Java7, Java7 first expands and then inserts new values, while Java8 interpolates first and then expands, but this is not important.

Array expansion

The resize () method is used to initialize the array or expand the array. After each expansion, the capacity is doubled, and the data is migrated.

Get process analysis

Get is really too simple compared to put.

Calculate the hash value of key and find the corresponding array subscript: hash& (length-1) according to the hash value.

Determine whether the element at this position in the array is exactly what we are looking for, if not, take the third step

Determine whether the element type is TreeNode, if so, use the red-black tree method to get the data, if not, take the fourth step

Traverse the linked list until an equivalent (= = or equals) key is found

Thank you for reading! This is the end of the article on "how to use the HashMap of Java8". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report