TreeSet集合

来源:互联网 发布:macoffice办公软件 编辑:程序博客网 时间:2024/06/17 19:34

1,TreeSet集合是有序的
2,TreeSet集合是没有重复的
3,Treeset T必须实现Comparable接口,重写ComparaTo()方法
4,可以用TreeSet来去除List集合重复的数据。
package com.bean;
public class Person implements Comparable{
private String name;
private int age;
public Person() {
super();
}
public Person(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;
}
@Override
public int compareTo(Person p) {
int temp = this.name.compareTo(p.name);
return temp==0?this.age-p.age:temp;
}

}

}