A major feature of Set collection is uniqueness.
How does the Set collection guarantee the uniqueness of its elements?
Answer: The Set collection guarantees the uniqueness of elements relying on two methods: hashCode() and equals() .
Steps :
First see if the hashCode() value is the same
Same : keep going equals() method
Return true : If the element is repeated, it will not be added
Return false : If the element is not repeated, it will be added to the collection
Different: just add the element directly to the collection
Two major characteristics of TreeSet collection: unique and sorting
How to ensure that elements are unique? How to achieve element sorting?
Answer: TreeSet is implemented based on TreeMap . The underlying data structure of TreeMap collection is a red-black tree (a self-balancing binary search tree), which ensures that elements are stored and retrieved in a certain order.
How are elements stored?
When the first element is stored, it is directly stored as the root node
Starting from the second element, each element is compared from the root node
as a right child
as a left child
equal ignore it
This is a problem, when storing elements, how to judge the size? It is small? are equal?
Answer: TreeSe has two sorting methods: natural sorting and comparator sorting. As for which sorting method to use, it is determined by the construction method.
Natural sorting:
When creating a TreeSet , it is required to construct with empty parameters. The class to which the objects to be sorted belong must implement the Comparable interface, override the compareTo() method and the compareTo() method, return an int type value, return 1 for large, and put it to the right; Return -1 , which means small, put it to the left; return 0 , which means equal, ignore it.
Basic data type wrapper classes, strings and other classes implement the Comparable interface and override the compareTo() method.
Source code analysis of the compareTo() method in the String class:
1. First get the shorter of the two strings, which is defined as minLength.
2. Convert the two strings into character arrays
. 3. According to minLength, traverse each character in the two character arrays and compare them one-to-one. If they are equal, compare the next pair of characters; if they are not equal, return the result of the subtraction of the two characters.
4. If the characters are equal within the minLength range, return the result of the subtraction of the lengths of the two strings.
Positive number: Large
negative number: small
0 : equal
Comparator sort:
When it is required to create a TreeSet , use a parameterized construction, the parameter is the implementation class of the comparator (interface), and the compare() method and the compare() method should be rewritten to return an int type value, return 1 for large, and put it to the right ; Return -1 , which means small, put it to the left; return 0 , which means equal, ignore it.
How are elements taken out?
Inorder traversal fetches elements.