在讲Set接口中 TreeSet 实现类的使用之前,先讲一下比较器的使用。
比较器的使用
【1】以int类型为案例
比较的思路:将比较的数据做差,然后返回一个int类型的数据,将这个int类型的数值 按照 =0 >0 <0 进行输出。
//比较int型
int a = 10;
int b = 20;
System.out.println(a-b);
【2】比较String类型数据
String类实现了Comparable接口,这个接口中有一个抽象方法compareTo,String类中重写这个方法即可。
//比较String型
String a = "A";
String b = "B";
//System.out.println(a-b);//无法这样比较
//String类重写了其实现:Comparable中的compareTo()方法:
System.out.println(a.compareTo(b));
【3】比较double类型数据
//3、比较double类型数据
double a = 5.6;
double b = 9.8;
System.out.println((int)(a-b)); //但是遇到5.6-5.3这样的就把0.3置为0了
//这样不行
//4、参考上面的String:实现接口,重写方法
//把a转成对应的Double包装,而Double类也是实现了接口,重写方法
System.out.println(((Double) a).compareTo((Double) b));
//即:最终都用compareTo()来比较
【4】比较自定义的数据类型
(1)内部比较器:
(内部比较器:即一个类实现了一个接口,并且所有的比较逻辑都在当前这个类的内部进行实现的)
代码示例:
自定义Student类:
package test10_comparator;
/**
* @Auther: zhoulz
* @Description: test10_comparator
* @version: 1.0
*/
public class Student implements Comparable<Student>{//实现了这个接口,就得对应去重写方法
private int age;
private double height;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int age, double height, String name) {
this.age = age;
this.height = height;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", height=" + height +
", name='" + name + '\'' +
'}';
}
@Override
public int compareTo(Student o) {
//return 0; //比较的话,比什么呢(Student有3个属性呢)?
//按照年龄age进行比较
//return this.getAge() - o.getAge();//不能直接用this.age、o.age
//按照身高height进行比较
//return ((Double)(this.getHeight())).compareTo((Double)(o.getHeight()));
//按照姓名name进行比较
//字符串直接调用compareTo()
return this.getName().compareTo(o.getName());
}
}
测试类:
package test10_comparator;
/**
* @Auther: zhoulz
* @Description: test10_comparator
* @version: 1.0
*/
public class Test2 {
public static void main(String[] args) {
//比较两个学生:
Student s1 = new Student(25,165.2,"xiaoming");
Student s2 = new Student(20,178.1,"xhxiaodong");
//想要进行比较,就得在自定义Student类中进行compareTo()的重写
System.out.println(s1.compareTo(s2));
}
}
(2)外部比较器:
代码示例:
自定义Student类:
package test11_comparator_external;
import java.util.Comparator;
/**
* @Auther: zhoulz
* @Description: test10_comparator
* @version: 1.0
*/
public class Student {
private int age;
private double height;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int age, double height, String name) {
this.age = age;
this.height = height;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", height=" + height +
", name='" + name + '\'' +
'}';
}
}
//外部比较器 —— 放在(Student类)外部了!!!
//比较年龄:
class bijiao01 implements Comparator<Student>{
//实现的是Comparator这个接口了
//同样需要重写方法:
//重写的是compare()这个方法了
@Override
public int compare(Student o1, Student o2) { //传入两个对象
//return 0;
//比较年龄:
return o1.getAge() - o2.getAge();
//也可以比较姓名——可以将上面注释,
//但是没有必要,重新写一个比较即可即可
}
}
//比较姓名:
class bijiao02 implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) { //传入两个对象
//return 0;
//比较姓名:
//字符串直接调用 compareTo()方法
return o1.getName().compareTo(o2.getName());
}
}
class bijiao03 implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
//return 0;
//在年龄相同的情况下,比较身高, 年龄不同比较年龄
if ((o1.getAge() == o2.getAge())){ //年龄一样
return ((Double)(o1.getHeight())).compareTo((Double)(o2.getHeight()));
}else{//年龄不一样
return o1.getAge() - o2.getAge();
}
}
}
测试类:
package test11_comparator_external;
import test11_comparator_external.Student;
import java.util.Comparator;
/**
* @Auther: zhoulz
* @Description: test10_comparator - 外部比较器
* @version: 1.0
*/
public class Test2 {
public static void main(String[] args) {
//比较两个学生:
Student s1 = new Student(25,165.2,"xiaoming");
Student s2 = new Student(20,178.1,"xhxiaodong");
//使用外部比较器
//首先要获取外部比较器:
//bijiao01 bj1 = new bijiao01();
//或者写作:接口等于它的实现类:
//比较年龄:
Comparator bj1 = new bijiao01();
System.out.println(bj1.compare(s1, s2));
//比较姓名:
Comparator bj2 = new bijiao02();
System.out.println(bj2.compare(s1, s2));
Comparator bj3 = new bijiao03();
System.out.println(bj3.compare(s1, s2));
}
}
【5】外部比较器和内部比较器相比较,哪一个较好?
答案:外部比较器,多态,扩展性好。