In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to carry out IdentityHashMap collection source code analysis, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
We'll introduce you to the IdentityHashMap collection. From the name we know that the IdentityHashMap collection is implemented through the HashMap collection at the bottom. Then, as we analyze other collections, it must have some special characteristics, otherwise Java would not provide a separate IdentityHashMap collection. Let's not go into the characteristics of IdentityHashMap collection, let's look at the following example first, and then do more details.
Looking at the output We are not surprised by the output of the HashMap collection, but the output of the IdentityHashMap collection is confusing. Because according to our previous analysis of the collection, it is not said that duplicate keys are not allowed to exist in HashMap. If there is the same key, the value of the element added to the collection will overwrite the value of the previous element. This is also the characteristic of HashMap summarized in the article on analyzing HashMap. So why is it possible to store two identical keys in an IdentityHashMap collection? The answer is simple, and that's what characterizes IdentityHashMap collections.
When we analyze the underlying implementation of HashMap, we know that in the HashMap collection, we will first determine whether the hashCodes of keys are equal, and then determine whether the keys are equal. If these two judgments are equal, then the keys of the two currently stored elements are considered equal, and the overwrite operation of value is performed. And the above judgment will not only judge == but also use the equals method to judge. This is also the root cause of why the equals method must be overridden when storing object-type data in a HashMap collection. If you do not do this, the HashMap collection will assume that the keys of these two elements are not the same. This is the fundamental difference between the low-level implementation of IdentityHashMap collection and HashMap collection, because in IdentityHashMap collection, only == will be used to determine whether two keys are equal, and the equals method of key will not be called to determine. If two keys == are equal, the IdentityHashMap collection assumes that the two keys are equal, and the value override operation is performed. If the keys == stored in the IdentityHashMap collection are not equal, the elements corresponding to the two keys are stored in the collection, and the value overwrite operation is not performed.
In the above code we create two String objects, and we know that in Java they actually compare memory addresses, if not primitive data types with == comparisons. Because we created two String objects, the comparison above must be false. The IdentityHashMap collection will think that these two keys are different keys, so both elements will be saved in the collection. In addition to the above comparison, the HashMap collection also calls the equals method of key for comparison. Because the key we save is a String, and the equals method has been overridden in the String class, what we call the method to actually compare is the real value, that is, key and key are compared. It is precisely because they are equal that the HashMap collection performs an override of value. So the HashMap collection only holds one element in the output result. Let's take a look at the underlying source code of the IdentityHashMap collection to prove what we said above.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.