Comparable和Comparator都是用于比较数据大小的,Comparable位于java.lang包下,实现该接口需要重写compareTo方法;Comparator位于java.util包下,实现该接口需要重写compare方法。
Comparable接口只有一个方法:
public int compareTo(T o);
调用此方法的对象,也就是this和o进行比较,若返回值大于0,则this大于o;返回值等于0则this等于o;返回值小于0则this小于o。因为this是需要一个明确的比较对象的,也就是一般情况下我们会在定义User类的时候有排序的需求,所以我们定义的类需要实现此接口。
package com.dalingjia.bean;
import java.util.*;
public class Student implements Comparable<Student> {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
//先比较name,name相同在比较age
if(this.name.compareTo(o.name) == 0){
if(this.age.compareTo(o.age) == 0){
return 0;
}else if (this.age > o.age){
return 1;
}else {
return -1;
}
}else if(this.name.compareTo(o.name) > 0){
return 1;
}else {
return -1;
}
}
public static void main(String[] args) {
List list = new ArrayList<Student>();
list.add(new Student("gol",21));
list.add(new Student("gol",19));
list.add(new Student("xiao",21));
list.add(new Student("long",21));
System.out.println("排序前:"+list);
//排序规则:先按name排序,若name相等则再比较age
Collections.sort(list);
System.out.println("排序后:"+list);
}
}
打印结果:
排序前:[Student{name='gol', age=21}, Student{name='gol', age=19}, Student{name='xiao', age=21}, Student{name='long', age=21}]
排序后:[Student{name='gol', age=19}, Student{name='gol', age=21}, Student{name='long', age=21}, Student{name='xiao', age=21}]
Comparator接口有一个重要的方法:
int compare(T o1, T o2);
compare比较的o1和o2,返回值大于0则o1大于o2,依次类推。
package com.dalingjia.bean;
import java.util.*;
public class Student {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
static class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.name.compareTo(o2.name) == 0) {
if (o1.age == o2.age) {
return 0;
} else if (o1.age > o2.age) {
return 1;
} else {
return -1;
}
} else if (o1.name.compareTo(o2.name) > 0) {
return 1;
} else {
return -1;
}
}
}
public static void main(String[] args) {
List list = new ArrayList<Student>();
list.add(new Student("gol", 21));
list.add(new Student("gol", 19));
list.add(new Student("xiao", 21));
list.add(new Student("long", 21));
System.out.println("排序前:" + list);
//排序规则:先按name排序,若name相等则再比较age
Comparator comparator = new StudentComparator();
Collections.sort(list, comparator);
System.out.println("排序后:" + list);
}
}