自然排序:
执行无参构造方法:TreeSet< T >():无参构造
要求当前自定义类型需要实现Comparable接口,重写comparesTo方法
它的主要实现方式如以下代码所示:
Student类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public class Student implements Comparable<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; }
public int compareTo(Student s){ int num = this.age - s.age ; int num2 = (num == 0)? (this.name.compareTo(s.getName())) : num; return num2; }
}
|
TestSetDemo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import java.util.TreeSet;
public class TreeSetDemo02 { public static void main(String[] args) { TreeSet<Student> ts = new TreeSet<Student>(); Student s1 = new Student("gaoyuanyuan",37); Student s2 = new Student("gaoyuanyuan",37); Student s3 = new Student("dengchao",35); Student s4 = new Student("wuqilong",35); Student s5 = new Student("wuyifan",21); Student s6 = new Student("liudehua",47); Student s7 = new Student("chenglong",52); ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); ts.add(s6); ts.add(s7); for (Student student : ts) { System.out.println(student.getName()+"-------"+student.getAge()); } } }
|
比较器排序:
执行有参构造方法:TreeSet< Student >(Comparator< Student > com):
- 方式1:自定义一个类实现Comparator接口中 的compare(T t1,T t2)方法
- 方式2:通过接口匿名内部类实现
具体举例如以下代码所示(学生类均如自然排序中的学生类所示):
方式1:
TreeSetDemo类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class TreeSetDemo { public static void main(String[] args) { Set<Student> set = new TreeSet<Student>(new MyComparator<Student>()); Student s1 = new Student("zhangsan" , 41); Student s2 = new Student("zhangsan" , 41); Student s3 = new Student("lisi" , 35); Student s4 = new Student("wangwu" , 35); Student s5 = new Student("zhangsan" ,28); Student s6 = new Student("laoliu" ,20); Student s7 = new Student("dage" ,52); set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); set.add(s6); set.add(s7); for (Student student : set) { System.out.println(student.getName() + "--------" + student.getAge()); } } }
|
Comparator接口的具体实现类(MyComparator类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.yk.treeset_02;
import java.util.Comparator;
public class MyComparator implements Comparator<Student> {
@Override public int compare(Student s1, Student s2) { int num = s2.getAge() - s1.getAge();
int num2 = (num == 0) ? (s1.getName().compareTo(s2.getName())) : num; return num2; }
}
|
方式2:
具体实现类(TreeSetDemo):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class TreeSetDemo { public static void main(String[] args) {
Set<Student> set = new TreeSet<Student>(new Comparator<Student>(){ @Override public int compare(Student s1, Student s2) { int num = s2.getAge() - s1.getAge(); int num2 = (num == 0)? (s1.getName().compareTo(s2.getName())) : num ; return num2; } }); Student s1 = new Student("zhangsan" , 41); Student s2 = new Student("zhangsan" , 41); Student s3 = new Student("lisi" , 35); Student s4 = new Student("wangwu" , 35); Student s5 = new Student("zhangsan" ,28); Student s6 = new Student("laoliu" ,20); Student s7 = new Student("dage" ,52); set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); set.add(s6); set.add(s7); for (Student student : set) { System.out.println(student.getName() + "--------" + student.getAge()); } } }
|