TreeSet排序的两种方法
1.自然排序
实现Comparable接口,重写compareTo()方法
import java.util.Set;
import java.util.TreeSet;
//TreeSet的自然排序
//1.实体类实现Comparable接口,重写compareTo()方法
public class TestTreeSetSort1 {
public static void main(String[] args) {
Set<Student> tree=new TreeSet();
Student s1=new Student("zs",21,88);
Student s2=new Student("zs",21,34);
Student s3=new Student("zl",24,99);
Student s4=new Student("ls",27,13);
Student s5=new Student("ls",22,65);
tree.add(s1);
tree.add(s2);
tree.add(s3);
tree.add(s4);
tree.add(s5);
//Exception in thread "main" java.lang.ClassCastException: com.qianfeng.kxf.day25.set.Student cannot be cast to java.lang.Comparable
//为什么报错?因为添加的是对象元素,必须实现一个排序,否则就会类型转换错误的问题
System.out.println("元素对象多少个"+tree.size());
for(Student s:tree) {
System.out.println(s);
}
}
}
class Student implements Comparable{
private String name;
private int age;
private int score;
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;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
public Student(String name, int age, int score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Student() {
super();
}
/*//重写该方法:只通过名字进行排序
@Override
public int compareTo(Object o) {
//先转换为Student类型对象
Student str=(Student)o;
//先对姓名进行排序,直接调用String类型中的compareTo()方法进行比较
return this.name.compareTo(str.name);
}
// 元素对象多少个3
// Student [name=ls, age=27, score=13]
// Student [name=zl, age=24, score=99]
// Student [name=zs, age=21, score=88]
*/
/*//如果名字相同,就按年龄排序
public int compareTo(Object o) {
Student stu=(Student)o;
//先对名字排序:升序
int n=this.name.compareTo(stu.name);
//如果名字相同就比较年龄 :升序
return n==0?this.age-stu.age:n;
}
// 元素对象多少个4
// Student [name=ls, age=22, score=65]
// Student [name=ls, age=27, score=13]
// Student [name=zl, age=24, score=99]
// Student [name=zs, age=21, score=88]
*/
//如果名字相同,就按年龄排,如果年龄还相同就按成绩排
public int compareTo(Object o) {
Student stu=(Student)o;
int n=this.name.compareTo(stu.name);
int a=n==0?this.age-stu.age:n;
return a==0?this.score-stu.score:a;
}
}
2.比较器排序
单独写一个比较器类,实现Comparator接口,重写compare()方法
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class TestTreeSetSort2 {
public static void main(String[] args) {
Set<Person> tree=new TreeSet(new MyComparator());
Person p1=new Person("zs",22,88);
Person p2=new Person("zs",22,83);
Person p3=new Person("ls",27,44);
Person p4=new Person("ww",22,38);
Person p5=new Person("ls",22,78);
tree.add(p1);
tree.add(p2);
tree.add(p3);
tree.add(p4);
tree.add(p5);
System.out.println("元素数量:"+tree.size());
for(Person p:tree) {
System.out.println(p);
}
}
}
class MyComparator implements Comparator<Person>{
//按姓名排序
/*@Override
public int compare(Person o1, Person o2) {
int n=o1.getName().compareTo(o2.getName());
return n;
}*/
//按姓名和年龄排序
/*public int compare(Person p1,Person p2) {
int n=p1.getName().compareTo(p2.getName());
return n==0?p1.getAge()-p2.getAge():n;
}*/
//按姓名.年龄.成绩排序
public int compare(Person p1,Person p2) {
int n=p1.getName().compareTo(p2.getName());
int a=n==0?p1.getAge()-p2.getAge():n;
return a==0?p1.getScore()-p2.getScore():a;
}
}
class Person{
private String name;
private int age;
private int score;
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;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
//如果不重写toString()方法,调用Object里面的toString()方法打印的是对象全类名+@+堆中经过hash运算之后的16进制数字
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", score=" + score + "]";
}
public Person(String name, int age, int score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Person() {
super();
}
}
通过匿名内部类实现
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class TestTreeSetSort3 {
public static void main(String[] args) {
//直接通过匿名内部类实现
Set<Person> tree=new TreeSet<Person>(new Comparator<Person>() {
//按姓名.年龄.成绩排序
public int compare(Person p1,Person p2) {
int n=p1.getName().compareTo(p2.getName());
int a=n==0?p1.getAge()-p2.getAge():n;
return a==0?p1.getScore()-p2.getScore():a;
}
});
Person p1=new Person("zs",22,88);
Person p2=new Person("zs",22,83);
Person p3=new Person("ls",27,44);
Person p4=new Person("ww",22,38);
Person p5=new Person("ls",22,78);
tree.add(p1);
tree.add(p2);
tree.add(p3);
tree.add(p4);
tree.add(p5);
System.out.println("元素数量:"+tree.size());
for(Person p:tree) {
System.out.println(p);
}
}
}
那怎么保证元素的唯一性?
这个跟TreeSet的底层结构有关,其实TreeSet在add元素的时候,实际上是通过TreeMap的put()方法去实现,它将我们的set元素作为Map中的key值,value值为空的去存储。这就可以明白为什么TreeSet在添加元素的时候为什么不能重复,因为Map在put元素的时候遇到同样的key值的元素时会进行覆盖。
TreeSet在存储元素的时候,是先创建一颗红黑树(特殊的自平衡排序二叉树),并将第一个元素作为树的根节点,后面添加的元素跟根节点的元素对比,如果比根节点的元素小,就成为根节点左子树;如果比根节点的元素大,就成为根节点的右子树;如果与根节点的元素相等,就不添加进来,这就是为什么不能存在相同元素的原因了。之后添加的元素都按照这个规则来存储