Set
(1) Features of Set set
Unordered (deposit and Fetch order), unique
(2) 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
C: How to guarantee the uniqueness of the element?
Guaranteed by Hashcode () and Equals ()
Note: If you need to save an object of a class to the HashSet collection, overriding the Equals () method and the Hashcode () method is to ensure that the return value of the hashcode is true when the object returns a value of true through the Equals () method;
Each "slot" slot in HashSet that can store an element is often referred to as the bucket bucket, and the Equals () method returns False if there are multiple elements with the same hashcode value. You need to put multiple elements in a "bucket", which can cause performance degradation;
(3) 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
Java Basics (vii)---set