Detailed explanation of Java set class source code analysis
Like list, set inherits from the collection interface. The commonly used implementation classes are HashSet and TreeSet. It is worth noting that HashSet is implemented through HashMap, while TreeSet is implemented through treemap. Therefore, both HashSet and TreeSet do not have their own data structure, which can be summarized as follows:
• the elements in the set cannot be repeated, that is, the elements are unique
• HashSet is stored according to the hash value of the element, so it is unordered, and at most one null object is allowed
• TreeSet is stored according to the size of elements, so it is ordered, and null objects are not allowed
• set collection has no get method, so it can only traverse elements through iterators, not random access
1.HashSet
The following is a partial source code of HashSet to understand its implementation.
Looking at the source code, we know that the data of HashSet is stored in the instance object map of HashMap and corresponds to the key in the map, while the reference present of object type is a virtual value corresponding to the value in the map, which has no practical significance. Associating with some features of HashMap: unordered storage, unique key value, etc., we can naturally understand the non repetition of set elements and the unordered storage of HashSet.
Here is the basic usage of HashSet from the perspective of source code:
• constructors (four)
1. Hashset() is an empty constructor and initializes an empty HashMap
2. HashSet (collection C) passes in a subset C to initialize HashMap
3. HashSet (int initialCapacity, float LoadFactor) initializes an empty HashMap and specifies the initial capacity and load factor
4. HashSet (int initialCapacity) initializes an empty HashMap and specifies the initial capacity
• insert elements
1. Add (E) insert the specified element (call the put method implementation of HashMap)
• find elements
1. Contains (object o) determines whether the collection contains the specified element (implemented by calling the containskey method of HashMap)
2. Since there is no get method in the implementation class of HashSet, it can only be traversed in turn through the iterator instead of random access (call the iterator implementation of keyset in HashMap)
Application example:
• modify elements
Because the key value in the HashMap cannot be modified, the HashSet cannot modify elements
• delete elements
1. Remove (object o) delete the specified element (call the remove method in HashMap to implement, and the return value is true or false)
2. Clear () clears the element (it is implemented by calling the clear method in HashMap, and there is no return value)
2.TreeSet
TreeSet is the only implementation class of the sortedset interface. As mentioned earlier, TreeSet does not have its own data structure, but is implemented through treemap. Therefore, TreeSet is also a storage structure based on red black binary tree. Therefore, TreeSet does not allow null objects and is stored in order (ascending order by default).
Navigablemap in the above source code is an interface inherited from srotedmap, and its implementation class is treemap. Therefore, the data in TreeSet is stored through treemap, and the present here is also a virtual value with no practical significance.
Here is the basic usage of HashSet from the perspective of source code:
• constructors (four)
1. Treeset() is an empty constructor. It initializes an empty treemap, which is arranged in ascending order by default
2. TreeSet (comparator comparator) passes in a user-defined comparator, which is often used to implement descending arrangement
3. TreeSet (collection C) passes in a subset C, which is used to initialize the treemap object in ascending order by default
4. TreeSet (sortedset < E > s) passes in an ordered subset S, which is used to initialize the treemap object and adopt the comparator of the subset
Application example
• insert elements
1. Add (E) insert the specified element (call the put method implementation of treemap)
2. Addall (collection C) inserts a subset C
• find elements
1. Contains (object o) judge whether the collection contains the specified object (call the containskey method of treemap to implement it)
2. Like HashSet, there is no get method in the implementation class of TreeSet, so it can only be traversed in turn through the iterator instead of random access (calling the iterator implementation of keyset in treemap).
• modify elements
TreeSet cannot modify elements for the same reason as HashSet.
• delete elements
1. Remove (object o) delete the specified element (call the remove method in treemap to implement, and return true or false)
2. Clear() clears the element (it is implemented by calling the clear method in treemap, and there is no return value)
Application example:
So far, the storage structure and basic usage of HashSet and TreeSet have been introduced.
The above detailed explanation of Java collection class source code analysis is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.