Some questions about TreeSet
Problem scenario:
TreeSet
It uses an ordered binary tree for access, which is ordered and in natural order. If it is not in natural order, there will be some minor problems.
Problem Description:
The default natural order of the storage system
package train4.train4_3;
import java.util.*;
public class treeset {
public static void main(String[] args) {
TreeSet<Integer> data = new TreeSet<>();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
data.add(0);
for(int i:data) {
System.out.println(i);
}
}
}
Use the forEach iteration loop through output
结果:
0
1
2
3
4
When the TreeSet
data we create is not the default natural data of the system but custom Person
content, problems will arise, for example.
package train4.train4_3;
import java.util.*;
public class treeset2 {
public static void main(String[] args) {
TreeSet<Person> data = new TreeSet<>();
Person p1 = new Person("张三",18);
Person p2 = new Person("李四",20);
Person p3 = new Person("王二",17);
data.add(p1);
data.add(p2);
data.add(p3);
for(Person p:data) {
System.out.println(p);
}
}
//定义Person类
static class Person{
private String name;
private int age;
public Person(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;
}
}
}
A Person
type of TreeSet
data is defined here .
TreeSet<Person> data = new TreeSet<>();
operation result:
java.lang.ClassCastException
Type conversion exception, Which means treeset2
that the Person
class in the file is Comparable
converted to a class conversion failure
Cause Analysis:
Because the data we store is a self-defined Person
type, of course, it cannot be realized by a system-defined method, and the system does not know how to sort the data you define.
solution:
Comparable
It is an interface, and we can implement this interface to rewrite, and we can specify the rules of comparison by ourselves.
static class Person implements Comparable<Person>
An error will still be reported at this time because the interface is abstract and we need to implement abstract methods.
@Override
public int compareTo(Person arg0) {
// TODO Auto-generated method stub
return 0;
}
Now we come to solve the problem and rewrite this compareTo
method.
The principle of comparison is to
Person
call acompareTo
method, and then pass in another data to be compared for comparison. In fact, it isthis
compared with andarg0
.
@Override
public int compareTo(Person arg0) {
//this 与 arg0 在比较
//返回的数据:负数(表示this小)||零(表示一样大)||正数(表示this大)
if(this.age>arg0.age) {
return 1;
}else if(this.age==arg0.age){
return 0;
}
return -1;
}
The complete code is as follows:
package train4.train4_3;
import java.util.*;
public class treeset2 {
public static void main(String[] args) {
TreeSet<Person> data = new TreeSet<>();
Person p1 = new Person("张三",18);
Person p2 = new Person("李四",20);
Person p3 = new Person("王二",17);
data.add(p1);
data.add(p2);
data.add(p3);
for(Person p:data) {
System.out.println(p);
}
}
static class Person implements Comparable<Person>{
private String name;
private int age;
@Override
public int compareTo(Person arg0) {
//this 与 arg0 在比较
//返回的数据:负数(表示this小)||零(表示一样大)||正数(表示this大)
if(this.age>arg0.age) {
return 1;
}else if(this.age==arg0.age){
return 0;
}
return -1;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public Person(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;
}
}
}
operation result:
Person [name=王二, age=17]
Person [name=张三, age=18]
Person [name=李四, age=20]
We know that it TreeSet
is Set
a kind of collection, and the Set
collection has a characteristic: duplicate data is not allowed to exist, if the data is stored.
Person p1 = new Person("张三",18);
Person p2 = new Person("李四",20);
Person p3 = new Person("王二",17);
Person p4 = new Person("麻子",17);
data.add(p1);
data.add(p2);
data.add(p3);
data.add(p4);
Then the data of Mazi failed to store, because when the compareTo
method was in progress , the return time was 0, and the storage failed. The running result is as follows:
Person [name=王二, age=17]
Person [name=张三, age=18]
Person [name=李四, age=20]