Java的比较接口 Comparable与Comparator

时间:2022-07-19 02:50:33

我们知道Comparable与Comparator接口都能使你的对象拥有你希望的顺序,那么为什么设计两个接口呢?

我们看一下Comparable的使用场景:

package com.dusk;

/**
* Created by dushangkui on 2017/6/9.
*/
public class Student implements Comparable<Student> {

private String name;
private int age;
private String sex;

public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}

/*
* 先比较学生的年龄,年龄小的排在前面 如果年龄一样再比较性别 女生排在前面
*/
@Override
public int compareTo(Student o) {
if(this.age>o.getAge())
return 1;
else if(this.age==o.getAge()){
if(this.sex==o.getSex()){
return 0;
}else if("女".equals(o.getSex())){
return 1;
}
}
return -1;
}

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 String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}
}

场景类:

package com.dusk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;

/**
* Created by dushangkui on 2017/6/9.
*/
public class Client {
public static void main(String[] args) {
Student stu1 = new Student("张三",22,"男");
Student stu2 = new Student("李四",20,"男");
Student stu3 = new Student("王五",20,"女");
Student stu4 = new Student("孙六",26,"女");
List<Student> students = new ArrayList<Student>(4);
students.add(stu1);
students.add(stu2);
students.add(stu3);
students.add(stu4);

System.out.println("原始数组: " + students);
Collections.sort(students);
System.out.println("排序后列表: " + students);

}
}

结果:

Java的比较接口 Comparable与Comparator
从上面我们可以看出来,Student类具有排序能力是通过实现Comparable接口,需要实现compareTo方法。


我们再看Comparator,现在有这样一个场景,我手里面有几本书必须要读,但是我这个人比较懒,只喜欢读薄的书,看下面:

package com.dusk;

/**
* Created by dushangkui on 2017/6/9.
*/
public class Book {
private String name;
private int pageNum;

public Book(String name, int pageNum) {
this.name = name;
this.pageNum = pageNum;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPageNum() {
return pageNum;
}

public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}

@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", pageNum=" + pageNum +
'}';
}
}
场景类:

package com.dusk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* Created by dushangkui on 2017/6/9.
*/
public class BookClient {
public static void main(String[] args) {
Book b1 = new Book("Java入门到提高",560);
Book b2 = new Book("岛上书店",260);
Book b3 = new Book("维基全书",25560);

List<Book> books =new ArrayList<Book>(3);
books.add(b1);
books.add(b2);
books.add(b3);
System.out.println("原始顺序 "+ books);
Collections.sort(books, new Comparator<Book>(){
@Override
public int compare(Book b1, Book b2) {
return b1.getPageNum()-b2.getPageNum();
}
});
System.out.println("排序后顺序 "+ books);
}
}
看结果:

Java的比较接口 Comparable与Comparator

从结果可以看出来,我们也得到了相同的顺序,细心的童鞋可能已经发现了 我们的Book类并没有实现Comparable接口。

这是怎么实现呢,其实是通过策略模式。

其实两个接口都可以实现相同的目的,只是使用的场景不同:

在实体类编写的初期建议使用Comparable接口,如果不是自己的代码或者想重写排序逻辑请使用Comparator接口。