Features of Set set (compared to ArrayList)
Unordered, unique
Two sub-classes that mainly study it
HashSet Collection
A: The underlying data structure is a hash table (is an array of elements as linked lists)
B: Hash table bottom-dependent two methods: Hashcode () and Equals ()
Execution order:
First, compare whether the hash value is the same
Same: Continue to execute equals () method
Returns true: The element is duplicated, not added
return false: Add elements directly to the collection
Different: Add elements directly to the collection
If you want to customize the object for HashSet traversal, then we need to rewrite the equals algorithm and the Hashcode method.
TreeSet Collection
A: The underlying data structure is a red-black tree (a self-balancing two-fork tree)
B: Guaranteed Ordering of elements
A: Natural sorting (elements are comparative)
Implementing the comparable interface for the class to which the element belongs
B: Comparator sorting (collection with comparison)
Let the collection construct method receive comparator implementation class object
If you want to customize the object for TreeSet traversal, then we need to rewrite the CompareTo method.
17th Day of Java Learning (TreeSet HashSet)