Set
Unordered and non-repeated traversal can only use the iterator or enhance the for Loop (equivalent to the java compiler ).
1) hashSet: implemented using a hash table;
2) TreeSet: Implemented by sorting binary trees;
When using the contains method to determine whether an object is included in a set, you must first index the object to a specific space based on its hashCode value, and then compare it with the object in the space by calling the equals method. This targeted search method is more efficient than linear table comparison.
For objects that override the equals method, we generally need to properly override the hashCode method () inherited from the object class. Otherwise, two objects that may have equal values will be returned as false after comparison of equals. For example:
Point p1 = new Point (1, 2 );
Point p2 = new Point (1, 2 );
If the hashCode method is not properly written, and the hashCode values of p1 and p2 may differ, there is no chance to compare them.
To be continued...