In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to deal with HashMap in java when the insertion position is not empty, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.
The first is the source code:
You can see that there are three situations when the inserted position is not empty:
1. First determine whether the hash value of p (the element currently to be inserted) is consistent with the hash value of the data to be inserted. If so, determine whether the key is consistent. If all are consistent, assign p to e, and then end the judgment.
2. If condition 1 is not true, determine whether p is TreeNode (the actual TreeNode is the simple implementation of the red-black tree, and the red-black tree will explain the TreeMap), and if so, insert the value to be inserted directly into the node.
3. If the above two situations do not match, then enter the third (in this case, the p node is a linked list, which is stored in a chain). First, a for loop is defined, and you can see that the for loop does not explicitly specify an automatic exit condition, that is, the for loop exits manually.
First of all, let's look at the body of the for cycle.
(1) the first step is to determine whether p has the next one. If not, then construct the information to be inserted as a node, and then determine whether binCount is greater than or equal to TREEIFY_THRESHOLD-1 (TREEIFY_THRESHOLD is a constant defined in HashMap with a value of 8). If it is greater than this value, treeifyBin is called. This operation is to build a red-black tree, which will be discussed later in TreeMap, which will not be explained too much here. As for why? Because it is faster to traverse with linked lists when there is less data, but when there is more data, it will be slower to use linked lists, and in order to avoid slowing down the efficiency of HashMap, when there is a lot of data, it will be automatically replaced by a red-black tree here (you can avoid hash collision attacks to a certain extent), while JDK developers think that it is more when the data is greater than or equal to 8, and you need to use a red-black tree instead of the linked list. The for loop is interrupted after the above operation.
(2) if p has the next one, then go to another judgment to determine whether the key and hash values of the next node are the same as the key and hash values to be inserted, and if so, jump out of the loop directly.
(3) if both cases fail, the reference of the next node is assigned to the node p of the current loop, and then the loop continues until the loop exits.
After judging the above three cases, determine whether the e at this time is equal to null. If it is not equal to null, a conclusion is drawn after the above process that the key you want to put in already exists. You only need to replace the value corresponding to the original key with the new value.
The above is all the contents of the article "how to deal with HashMap when the insertion position of JDK in java is not empty". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.