TreeSet collection in-depth understanding of the attack principles, treeset --------
Set Interface
Set does not allow identical elements. If you try to add two identical elements to the same Set, the add method returns false. (Unordered, repeatable)
Set determines whether two objects are the same, instead of using the = Operator, but based on the equals method. That is to say, as long as the two objects are compared using the equals method and return true, the Set will not accept the two objects.
HashSet
HashSet has the following features:
The arrangement order of elements cannot be guaranteed, and the order may change.
Not synchronized
The Set element can be null, but only one null element can be put.
When an element is stored in a HashSet combination, HashSet calls the hashCode () method of the object to obtain the hashCode value of the object, then, the location of the object stored in the HashSet is determined based on the hashCode value.
To put it simply, the HashSet set sets judge that two elements are equal. The two objects are equal through the equals method, and the hashCode () method return values of the two objects are equal.
Note: If you want to put an object in a HashSet and override the equals method of the corresponding class of the object, you should also rewrite its hashCode () method. The rule is that if two pairs return true through the equals method, their hashCode should also be the same. In addition, the attributes used as equals comparison standards in the object should be used to calculate the hashCode value. (HashSet: a data structure hash table with non-synchronous threads. Determine whether the hashCode is the same. If the hashCode is the same, determine the equals method of the element .)
The TreeSet class has the same meaning in two ways.
1TreeSet is the only implementation class of the SortedSet interface. TreeSet can ensure that the collection elements are in the sorting state. TreeSet supports two sorting methods: Natural sorting and custom sorting. Natural sorting is the default sorting method. Objects of the same class should be added to the TreeSet.
TreeSet determines that two objects are not equal by using the equals method to return false for the two objects, or by comparing the CompareTo method, no 0 is returned.
Natural sorting
For natural sorting, use the CompareTo (Object obj) method of the elements to be sorted to compare the size relationship between elements, and then sort the elements in ascending order.
Java provides a Comparable interface, which defines a compareTo (Object obj) method. This method returns an integer and the Object of this interface can be compared.
If the obj1.compareTo (obj2) method returns 0, the two objects to be compared are equal. If a positive number is returned, obj1 is greater than obj2. If it is a negative number, obj1 is smaller than obj2.
If we always return true for the equals method of the two objects, the compareTo method of the two objects returns 0.
Custom sorting
The natural sorting is based on the size of the Set elements in ascending order. to customize the sorting, use the Comparator interface to implement the int compare (T o1, T o2) method.
2TreeSet: sorts the elements in the set. The underlying data structure is a binary tree. The basis for ensuring element uniqueness is: return 0 of compareTo.
Features of the TreeSet set:
Two sorting methods:
Method 1: Make the elements in the set have a comparison, so that the objects added to the TreeSet set must implement the comparable interface to override the compareTo (Object obj) method.
This method also becomes the natural or default sorting of elements. (However, if the sorting elements are not written by myself and others do not implement the comparable interface, the second method is used for sorting)
Method 2: make the collection container have a comparison, customize a comparator to implement the comparator interface, override the compare (Object o1, Object o2) method, and
The custom comparator is used as a parameter to pass to the constructor of the container so that the Set container has a comparison. The priority of this method is higher than that of method 1,