Set Interface Usage in java and ET Interface Usage
The set interface in java has the following features:
- Repeated elements are not allowed;
- The element positions in the set are unordered;
- There is only one element with a null value.
Because the set interface in java imitates the set abstraction in mathematics, the corresponding set features in mathematics are:
- Interaction: In a collection, any two elements are considered to be different, that is, each element can only appear once.
- Sequent: In a collection, each element has the same status and unordered elements. The set can define the order relationship. After the order relationship is defined, the elements can be sorted by the order relationship. But there is no sequence between elements in terms of the characteristics of the set itself.
- The nature of the empty set: the empty set is a subset of all sets.
Set does not save repeated elements. The most common use of Set is test normalization. You can easily ask whether an object is in a Set. Set has the same interface as Collection, so there is no additional function. Set is actually a Collection, but the behavior is different.
The main features of the Set interface are HashSet, TreeSet, and javashashset. Each of the same items is saved in one copy. They also have differences, as shown below:
1. HashSet:
HashSet uses a rather complex method to store elements. Using HashSet can obtain the elements in the set as quickly as possible, with high efficiency (Change Time by space ). Whether or not the pang end is the same object based on hashcode and equals. If the hashcode is the same and equals returns true, it is the same object and cannot be retained.
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 preceding example, after the hashCode () and equals () methods are rewritten to differentiate and agree to an object, the same object cannot be stored. If you annotate these two methods, all Student objects are considered different and can be stored.
2. TreeSet
TreeSet cannot store duplicate objects, but TreeSet Automatically sorts objects. If the stored objects cannot be sorted, an error is returned. Therefore, you must specify a sorting rule for the stored objects. Sorting rules include natural sorting and customer sorting.
① Natural sorting: java is implemented on the object class on which the TreeSet object to be added. lang. comparable interface, and override the comparaTo () method. If 0 is returned, it indicates the same object; otherwise, it indicates different objects.
② Customer sorting: Create a third-party class and implement the java. util. Comparator interface. And rewrite the method. Define the set form as 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); } }}
Output result:
1
2
3
4
5
The following example uses TreeSet to store objects sorted by customers:
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); } }}
Output result:
5
4
3
2
1
We all know that the List is stored in the order of insertion. In fact, you can also sort the List set by natural sorting and customer sorting. For details, refer:
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); // sorted by customer Collections. sort (list, new MySort1 (); System. out. println (list );}}
Output result:
[5, 1, 2, 4, 3]
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
This article Reprinted from: http://www.cnblogs.com/liuling/archive/2013/04/16/set.html