In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to parse the source code of HashSet, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Today we examine the underlying implementation of HashSet, because the underlying implementation of HashSet is implemented through HashMap. So the underlying HashSet is also stored through the data structure of the hash table. So here we will not introduce the knowledge of data structure, there are those who do not understand the underlying implementation of the hash table can browse HashMap this article. Next, as with other collections, we will analyze the underlying implementation of HashSet from the initialization aspect of HashSet.
Initialization
We see that in the no-parameter constructor in HashSet, a HashMap object is created directly. This proves what we said above, that the underlying HashSet is implemented through HashMap. We mentioned it in the article analyzing HashMap. If we use a no-parameter constructor to create a HashMap object, the underlying data structure is not initialized at this time, and the first initialization occurs when the put method is called for the first time. So we can say here that when we call the no-parameter constructor to create the HashSet object, the underlying data structure does not perform initialization. Then its first initialization should also occur in the first call to the method of adding elements, that is, the add method in HashSet. Let's take a look at the specific logic of this method.
The above code is the add method in HashSet, and we see that the logic of this method is to call the put method in HashMap directly and set the value of the added element to key,value in HashMap to store an Object object.
At this point, we know that HashSet has the following characteristics, which are:
In HashSet, there is no guarantee that the order in which elements are added is consistent with the order in which elements are traversed. Because the underlying layer is saved through the value of key in HashMap. Because the value of key in HashMap is calculated through the hash code of key. So there is no guarantee that the addition order is the same as the traversal order.
Because the underlying HashSet is saved through the value of key in HashMap, repeating elements cannot be saved in HashSet. Because the key in HashMap cannot be repeated. If a duplicate key is saved in HashMap, the value that is later added to the HashMap replaces the value that was first added to the HashMap.
Because HashMap is not a thread-safe collection class, and when we analyze the HashSet source code, we do not find that HashSet adds the additional synchronization keyword synchronized, so it means that HashSet is not a thread-safe collection class.
On how to parse the HashSet source code to share here, I hope that the above content can be of some help to 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.
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.