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 analyze HashMap Learning

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to analyze the learning of HashMap, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

HashMap stores data based on the hashCode value of the key, which in most cases can be located directly, so it has fast access speed, but the traversal order is uncertain. HashMap allows at most the key of one record to be null and the value of multiple records to be null. HashMap is not thread-safe, that is, multiple threads can write HashMap at any one time, which may lead to data inconsistency. If you need to meet thread safety, you can use Collections's synchronizedMap method to make HashMap thread-safe, or you can use ConcurrentHashMap.

I could tell at a glance that he had done his homework before the interview. Do not really understand HashMap, a put method to beat him back to the original!

Why do people who study well study well? Because others are good at summing up! All right, let's cut the crap and look at my picture!

To put it simply, it can be summarized in the following sentences!

1. Determine whether the key value pair table [I] is empty or null, otherwise execute resize () to expand the capacity.

two。 Calculate the array index I that hash is worth inserting according to the key value key. If table [I] = = null, add the new node directly, go to ⑥, and turn to ③ if table [I] is not empty.

3. Determine whether the first element of table [I] is the same as key. If the same directly overrides value, otherwise it turns to ④. The same here refers to hashCode and equals.

4. Determine whether table [I] is treeNode, that is, whether table [I] is a red-black tree. If it is a red-black tree, insert key-value pairs directly into the tree, otherwise turn to ⑤.

5. Traverse table [I] to determine whether the length of the linked list is greater than 8, and if it is greater than 8, convert the linked list into a red-black tree, and perform the insert operation in the red-black tree, otherwise, insert the linked list; if you find that key already exists, you can directly overwrite the value during traversal.

6. After the insertion is successful, determine whether the actual number of key-value pairs size exceeds the maximum capacity threshold, and if so, expand the capacity.

The answers to the learning questions on how to analyze HashMap are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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

Servers

Wechat

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

12
Report