Set set overview and features
Set set features
A collection that does not contain duplicate elements
There is no indexed method, so you can't use a normal for loop to traverse
Hash value
Hash value: it is a value of int type calculated by JDK according to the address, string or number of the object
There is a method in the Object class to get the hash value of the Object
public int hashCode(): returns the hash code value of the object
Hash value characteristics of object
The hashCode() method is called multiple times for the same object, and the returned hash value is the same
By default, different objects have different hash values. Rewriting the hashCode() method can make the hash values of different objects the same
HashSet collection overview and features
HashSet set features
1. The underlying data structure is hash table
2. There is no guarantee for the iterative order of the set, that is, the order of stored and extracted elements is not consistent
3. There is no indexed method, so ordinary for loop traversal cannot be used
4. Because it is a Set set, it is a Set that does not contain duplicate elements
HashSet collection storage elements
To ensure element uniqueness, you need to override hashCode() and equals()
Hash table of common data structures
Hashtable
Before JDK8, the bottom layer was implemented by array + linked list, which can be said to be an array with linked list elements
After JDK8, when the length is long, the bottom layer is optimized
LinkedHashSet collection overview and features
LinkedHashSet set features
1. The Set interface implemented by hash table and linked list has predictable iteration order
2. The linked list ensures the order of elements, that is, the storage and extraction order of elements are consistent
3. The hash table ensures that the elements are unique, that is, there are no duplicate elements
TreeSet collection overview and features
TreeSet set features
1. Elements are ordered. The order here does not refer to the order of storage and retrieval, but is sorted according to certain rules. The specific sorting method depends on the construction method
TreeSet(): sort according to the natural order of its elements
TreeSet(Comparator comparator): it is easy to sort with the specified comparator
2. There is no indexed method, so ordinary for loop traversal cannot be used
3. Because it is a Set set, it does not contain a Set of duplicate elements
Use of natural sorting Comparable
Store and traverse the student object, create a TreeSet collection, and use the parameterless construction method
Requirements: sort according to age from small to large. For the same age, sort according to the alphabetical order of names
Student class
public class Student implements Comparable<Student>{ private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int compareTo(Student student) { int num = this.age = student.age; int num2 = num==0?this.name.compareTo(student.name):num; return num2; } }
Test class
import java.util.TreeSet; public class TreeSetDemo01 { public static void main(String[] args) { // Create collection object TreeSet<Student> ts = new TreeSet<Student>(); // Create student object Student s1 = new Student("xishi",23); Student s2 = new Student("yangyuhuan",24); Student s3 = new Student("diaochan",20); Student s4 = new Student("wangzhaojun",22); Student s5 = new Student("linqingxia",24); // Add students to collection ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); // Traversal set for(Student s : ts){ System.out.println(s.getName() + "," + s.getAge()); } } }
conclusion
1. Use the TreeSet set to store custom objects. The parameterless construction method uses natural sorting to sort elements
2. Natural sorting means that the class to which the element belongs implements the Comparable interface and rewrites the comparaTo(T o) method
3. When rewriting, be sure to note that the sorting rules must be written according to the required primary and secondary conditions
Use of Comparator sorting Comparator
Store the student object and traverse it, create a TreeSet collection, and use the construction method with parameters
Requirements: sort according to the age from small to large. If the age is the same, sort according to the letter of the name
Student class
public class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Implementation class
import java.util.Comparator; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){ @Override public int compare(Student o1, Student o2) { int num = o1.getAge()-o2.getAge(); int num2 = num == 0?o1.getName().compareTo(o2.getName()):num; return num2; } }); // Create student object Student s1 = new Student("xishi",23); Student s2 = new Student("yangyuhuan",24); Student s3 = new Student("diaochan",20); Student s4 = new Student("wangzhaojun",22); Student s5 = new Student("linqingxia",24); // Add students to collection ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); // Traversal set for(Student s : ts){ System.out.println(s.getName() + "," + s.getAge()); } } }
conclusion
The user-defined objects are stored in the TreeSet collection. The construction method with parameters uses comparator sorting to sort the elements
Comparator sorting is to let the collection construction method receive the implementation class object of comparator and rewrite the compare (T o1, T o2) method
When rewriting a method, be sure to note that the collation must be written according to the required primary and secondary conditions