In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to analyze the TreeSet source code, the content is very detailed, interested friends can refer to, hope to be helpful to you.
As the name implies, the TreeSet collection that we introduce today is implemented at the bottom through the TreeMap collection. So needless to say, let's just look at the initialization code of TreeSet.
Initialization
Initialization, like HashSet, calls the parameterless constructor of the underlying implementation collection by default. That is, the TreeMap object is created directly, and we know through the analysis of the TreeMap collection that the initialization of the underlying data structure is not performed at this time, which means that the initialization of the underlying data structure will not be performed when we create the TreeSet object through the no-parameter construction method. Then the initialization action must also occur when the add method is called for the first time. Let's take a look at the underlying implementation of the add method in the TreeSet collection.
The implementation logic of the add method in HashSet is basically the same as that in HashSet. All the implementation classes of the underlying Map are called directly. That is, call the put method in TreeMap and set the value to Object. Since the underlying TreeSet is implemented through TreeMap, it is easy to conclude that TreeSet has the following characteristics:
Elements saved in the TreeSet collection must be sorted
The null element cannot be saved in the TreeSet collection, otherwise the virtual machine throws an exception
The TreeSet collection is an ordered collection class, but there is no guarantee that the order in which elements are added is consistent with the traversal order.
The TreeSet collection is not a thread-safe collection class. When using it in a multithreaded environment, additional synchronization code should be added to ensure thread safety.
On how to parse the TreeSet source code to share here, I hope 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.