【java】TreeSet、Comparable、Comparator、内部类、匿名类

时间:2020-11-28 00:56:27
 package com.tn.treeSet;

 public class Student {
private String name;
private int age;
public Student(){}
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;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
}

Student

 package com.tn.treeSet;

 import java.util.TreeSet;

 public class TreeSetDemo {
public static void main(String[] args){
TreeSet<Student> students=new TreeSet<>();//后一个尖括号内可以省略类型
Student student1=new Student("武松",30);
Student student2=new Student("林冲",31);
Student student3=new Student("鲁智深",29);
students.add(student1);
students.add(student2);
students.add(student3);
System.out.println(students);
}
}

TreeSetDemo

运行结果:

【java】TreeSet、Comparable、Comparator、内部类、匿名类

原因:TreeSet中加入的对象需要能进行比较,即实现Comparable接口


改造一:在Student类中实现Comparable接口

 package com.tn.treeSet;

 public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(){}
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;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
@Override
public int compareTo(Student o) {
if(!this.equals(o)){
// return this.name.compareTo(o.name);
return o.name.compareTo(this.name);//和上面语句打印顺序颠倒。
}
return 0;
}
}

Student

 package com.tn.treeSet;

 import java.util.TreeSet;

 public class TreeSetDemo {
public static void main(String[] args){
TreeSet<Student> students=new TreeSet<>();//后一个尖括号内可以省略类型
Student student1=new Student("武松",30);
Student student2=new Student("林冲",31);
Student student3=new Student("鲁智深",29);
students.add(student1);
students.add(student2);
students.add(student3);
System.out.println(students);
};//方法体结结尾大括号后有;不会报错
};//类体最后一个大括号后有;不会报错

TreeSetDemo


改造二:用内部类实现Comparator接口

 package com.tn.treeSet;

 public class Student{
private String name;
private int age;
public Student(){}
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;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
}

Student

 package com.tn.treeSet;

 import java.util.Comparator;
import java.util.TreeSet; public class TreeSetDemo {
public static void main(String[] args){
Student student1=new Student("武松",30);
Student student2=new Student("林冲",31);
Student student3=new Student("鲁智深",29);
//TreeSet构造时用Comparator作为构造函数参数
TreeSet<Student> students=new TreeSet<Student>(new ComparatorDemo());
students.add(student1);
students.add(student2);
students.add(student3);
System.out.println(students);
}
static class ComparatorDemo implements Comparator<Student>{
// 内部类要写在类体里,但不能写进类中方法体内。
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
}
}

TreeSetDemo


改造三:用匿名类实现Comparator接口

 package com.tn.treeSet;

 public class Student{
private String name;
private int age;
public Student(){}
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;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
}

Student

 package com.tn.treeSet;

 import java.util.Comparator;
import java.util.TreeSet; public class TreeSetDemo {
public static void main(String[] args) {
Student student1 = new Student("武松", 30);
Student student2 = new Student("林冲", 31);
Student student3 = new Student("鲁智深", 29); TreeSet<Student> students = new TreeSet<Student>(
new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
});
students.add(student1);
students.add(student2);
students.add(student3);
System.out.println(students);
}
}

TreeSetDemo

总结:

TreeSet容器中的对象要能排序,两种实现排序方法:

1.TreeSet使用无参构造函数,容器中的对象实现Comparable接口,见改造一;

2.TreeSet构造时使用Comparator作为构造函数参数;

  比较方法如果返回0,则对象不能重复加入。

TreeSet底层是TreeMap