The set interface in java has the following characteristics:
Repeated elements are not allowed;
The elements in the set are in no order;
There is and only one element with a value of null.
Because the set interface in java mimics the mathematical set abstraction, the corresponding mathematical set characteristics are:
Mutual opposite sex: In a set, any two elements are considered different, that is, each element can only appear once.
Disorder: In a set, each element has the same status and is disordered between elements. Sequential relationships can be defined on a set. After defining the order relationships, the elements can be sorted according to the order relationship. But in terms of the characteristics of the set itself, there is no necessary order between elements.
The nature of an empty set: an empty set is a subset of all sets
Set does not save duplicate elements. The most commonly used set is the test attributes, you can easily ask whether an object is in a set. Set has exactly the same interface as Collection, so there is no extra functionality. In fact, Set is a Collection, but the behavior is different.
The main things that implement the Set interface are HashSet, TreeSet, and LinkedHashSet, which are the common points of each same item are saved only one copy. They also have differences, the differences are as follows:
1.HashSet:
HashSet uses a very complex way to store elements. Using HashSet can get elements in the collection as fast as possible, which is very efficient (for space to trade time). Whether the Pangduan is the same object will be determined based on hashcode and equals. If the hashcode is the same and equals returns true, it is the same object and cannot be stored repeatedly.
package cn.set;import java.util.HashSet;import java.util.Set;class Student{ int id; public Student(int id) { this.id = id; } @Override public String toString() { return this.id+""; } @Override public int hashCode() { return this.id; } @Override public boolean equals(Object obj) { if (obj instanceof Student){ Student stu = (Student) obj; if (stu.id == this.id) return true; } return false; }}public class HashSetTest { public static void main(String[] args) { Set<Student> set = new HashSet<Student>(); Student s1 = new Student(1); Student s2 = new Student(1); Student s3 = new Student(2); set.add(s1); set.add(s2); set.add(s3); for (Student s : set) { System.out.println(s); } }}As shown in the above example, after rewriting the hashCode() and equals() methods to distinguish the consent objects, the same objects cannot be stored. If these two methods are annotated, all Student objects are considered different objects and can be stored.
2.TreeSet
TreeSet cannot store duplicate objects, but TreeSet will automatically sort. If the stored objects cannot be sorted, an error will be reported, so the stored objects must specify the sorting rules. Sorting rules include natural sorting and customer sorting.
① Natural sorting: The object to add TreeSet will implement the java.lang.Comparable interface on which object class, and override the comparaTo() method. Returning 0 means that it is the same object, otherwise it is a different object.
②Customer sorting: Create a third-party class and implement the java.util.Comparator interface. And rewrite the method. Define the collection form TreeSet ts = new TreeSet(new third-party class());
The following example uses TreeSet to store naturally sorted objects:
package cn.set;import java.util.Set;import java.util.TreeSet;class Student1 implements Comparable<Student1>{ int id; public Student1(int id) { this.id = id; } @Override public String toString() { return this.id+""; } @Override public int hashCode() { return this.id; } @Override public boolean equals(Object obj) { if (obj instanceof Student1){ Student1 stu = (Student1) obj; if (stu.id == this.id) return true; } return false; } public int compareTo(Student1 o) { return (this.id-o.id); }}public class TreeSetTest { public static void main(String[] args) { Set<Student1> set = new TreeSet<Student1>(); Student1 s1 = new Student1(5); Student1 s2 = new Student1(1); Student1 s3 = new Student1(2); Student1 s4 = new Student1(4); Student1 s5 = new Student1(3); set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); for (Student1 s : set) { System.out.println(s); } }} The output result is:
The following example uses TreeSet to store customer sorted objects:
package com.set;import java.util.Set;import java.util.TreeSet;class Student1 implements Comparable<Student1>{ int id; public Student1(int id) { this.id = id; } @Override public String toString() { return this.id+""; } @Override public int hashCode() { return this.id; } @Override public boolean equals(Object obj) { if (obj instanceof Student1){ Student1 stu = (Student1) obj; if (stu.id == this.id) return true; } return false; } public int compareTo(Student1 o) { return (this.id-o.id); }}public class TreeSetTest { public static void main(String[] args) { Set<Student1> set = new TreeSet<Student1>(); Student1 s1 = new Student1(5); Student1 s2 = new Student1(1); Student1 s3 = new Student1(2); Student1 s4 = new Student1(4); Student1 s5 = new Student1(3); set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); for (Student1 s : set) { System.out.println(s); } }} The output result is:
Everyone knows that List is sorted in the order of insertion when stored. In fact, you can also sort List collections by natural sorting and customer sorting. Please see:
package cn.set;import java.util.ArrayList;import java.util.Collections;import java.util.List;class MySort1 implements java.util.Comparator<Student3>{ public int compare(Student3 o1, Student3 o2) { return o2.id-o1.id; }}class Student3 implements Comparable<Student3>{ int id; public Student3(int id) { this.id = id; } @Override public String toString() { return this.id+""; } public int compareTo(Student3 o) { return (this.id-o.id); }}public class ListSort { public static void main(String[] args) { List<Student3> list = new ArrayList<Student3>(); Student3 s1 = new Student3(5); Student3 s2 = new Student3(1); Student3 s3 = new Student3(2); Student3 s4 = new Student3(4); Student3 s5 = new Student3(3); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); System.out.println(list); //Natural sorting: Collections.sort(list); System.out.println(list); //Customer sorting Collections.sort(list, new MySort1()); System.out.println(list); }} The output result is:
[5, 1, 2, 4, 3]
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
The following is a principle that the Set collection interface in Java implements no duplication of inserting objects:
In a Java collection, the rule to determine whether two objects are equal is:
1) Determine whether the hashCodes of the two objects are equal
If it is not equal, it is considered that the two objects are not equal. If it is equal, turn to 2)
(This is only required to improve storage efficiency. In fact, it is possible to have no theoretically, but if not, the efficiency will be greatly reduced during actual use, so we will make it necessary here. This issue will be focused on later.)
2) Determine whether the two objects are equally calculated using equals
If it is not equal, think that the two objects are not equal. If it is equal, think that the two objects are equal (equals() is the key to judging whether the two objects are equal)
For general class objects (except encapsulated objects such as String):
If the ordinary class does not override hashcode() and equals() methods, then when comparing the objects, the hashcode() method in the inherited object class, the hashcode() method in the object class is a local method. When comparing the return value of the method, the object address (reference address) is compared. Use the new method to create an object with the same content. Of course, different objects are generated twice. Unless the hashcode() method is overridden. The equals() method defined in the object class is also a comparison of object addresses. In a word: If you do not rewrite the hashcode() and equals() methods of ordinary classes, the object reference addresses are different in the Set collection, and the object will not be repeated.
For objects such as String (String, Integer, Double... etc.):
Since these encapsulation classes themselves have rewritten the hashcode() method, and the return value of the rewritten method is related to the content of the object, not to the reference address. The equals() method in these encapsulated classes is also rewritten, comparing the content of the object rather than the reference address. In a word, objects of classes such as String compare their content in the collection, and if the same content is overwritten, existing objects are covered.
The above is all about this article, I hope it will be helpful to everyone's learning.