The set interface
Set interface: Unordered and non-repeatable
- HashSet: the main implementation class for the set interface; Thread unsafe, can store NUll values
- LinkedHashSet: a subclass of HashSet that traverses its internal data in order of addition
- TreeSet: You can sort by attributes specified by added objects
There are no additional new methods defined in the Set interface
HashSet
On disorder and unrepeatability
Disorder:
Not equal to randomness. In the case of a HashSet, the data stored in the underlying array is not added in order of the array index, but by the hash value of the data
Non-repeatability:
Ensure that added elements cannot return true when judged by equals (). That is, only one element can be added to the same element. Understanding the process of adding elements together
The process of adding elements
To add element A to a HashSet, first call the hashCode () method and calculate the hash value of a.
- If no, the IP address is successfully added
- If there are other elements B (or more than one element in a linked list), the hashes of A and B are compared
- If the hash values are different, element A is added successfully
- If the hash values are the same, the equals method of a’s class is called
- Return true, a is the same as B, and the add fails
- Return false, adding succeeded
public class SetTest { public static void main(String[] args) { Set set = new HashSet(); set.add(456); set.add(123); set.add("AA"); set.add("CC"); set.add(new Person("Tom", 12)); set.add(129); // System.out.println(set); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }}}Copy the code
LinkedHashSet
- As a subclass of HashSet, LinkedHashSet maintains two references while adding data, which is more efficient for frequent traversal operations
- Inherit methods from the Collection interface
Public class LinkedHashSetTest {public static void main(String[] args) {public class LinkedHashSetTest {public static void main(String[] args) { Set Set = new LinkedHashSet(); Set Set = new LinkedHashSet(); set.add(456); set.add(123); set.add("AA"); set.add("CC"); set.add(new Person("Tom", 12)); set.add(129); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }}}Copy the code
TreeSet
- The data added to the TreeSet requires objects of the same class
- Two sorts: natural (implements the Comparable interface) and custom (Comparator)
- In natural sorting, the standard for comparing two objects is: compareTo() returns 0 instead of equals ().
- In custom sorting, the criterion for comparing two objects is: compare () returns 0, not equals ().
In TreeSet, the added data is de-sorted by red-black binary tree, and sorting requires rewriting of two comparison methods to specify the attributes of the sorted class.
public static void main(String[] args) { TreeSet set = new TreeSet(); // Set. Add (456); // set.add(123); // set.add("AA"); // set.add("CC"); // set.add(new Person("Tom", 12)); /* set.add(34); set.add(-34); set.add(43); set.add(11); set.add(8); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }*/ set.add(new Person("Tom", 12)); set.add(new Person("Jerry", 32)); set.add(new Person("Jim", 2)); set.add(new Person("Mike", 65)); set.add(new Person("Jack", 33)); set.add(new Person("Jack", 56)); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("**********************"); Comparator com=new Comparator() {@override public int compare(Object o1, Object o2) { if (o1 instanceof Person && o2 instanceof Person) { Person p1 = (Person) o1; Person p2 = (Person) o2; return Integer.compare(p1.getAge(), p2.getAge()); } else {throw new RuntimeException(" input type inconsistent "); }}}; TreeSet set2 = new TreeSet(com); set2.add(new Person("Tom", 12)); set2.add(new Person("Jerry", 32)); set2.add(new Person("Jim", 2)); set2.add(new Person("Mike", 65)); set2.add(new Person("Jack", 33)); set2.add(new Person("Jack", 56)); iterator = set2.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }}Copy the code