Java中的Comparator使用
Contents
Comparator 与 Comparable
比较器,一般用于排序的时候。即如果某个没有实现Comparable接口, 这时可以通过Comparator来实现类的外在排序器。 它与 Comparable 不同, 它一般是用于类自身,即类的内在排序。
Comparator 比较灵活点,可以通过需求,按指定的排序要求来进行排序。而实现Comparable则是Collections.sort(list)时,默认的排序。 这里可以通过一个简单的例子来说明。
比如,Person类,默认情况下,自身是可以按年龄来排序的。如:
Comparable
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class T3 {
public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>();
Random r = new Random();
Person p = null;
for (int i = 0; i < 5; i++) {
p = new Person();
p.setAge(r.nextInt(100));
p.setName("name" + i);
persons.add(p);
}
System.out.println("排序之前:");
printInfo(persons);
System.out.println("排序之后");
Collections.sort(persons);
printInfo(persons);
}
public static void printInfo(List<Person> list) {
for (Person person : list) {
System.out.println(" name = " + person.getName() + ", age = " + person.getAge());
}
}
}
class Person implements Comparable<Person> {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Person o) {
return this.age - o.age;
}
}
结果为:
排序之前:
name = name0, age = 71
name = name1, age = 64
name = name2, age = 9
name = name3, age = 52
name = name4, age = 22
排序之后
name = name2, age = 9
name = name4, age = 22
name = name3, age = 52
name = name1, age = 64
name = name0, age = 71
使用Comparator
改为Comparator,则可以动态按需排序。只需要构造相应
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
public class T3 {
public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>();
Random r = new Random();
Person p = null;
for (int i = 0; i < 5; i++) {
p = new Person();
p.setAge(r.nextInt(100));
p.setName("name" + i);
persons.add(p);
}
System.out.println("排序之前:");
printInfo(persons);
System.out.println("排序之后");
Collections.sort(persons, new PersonComparator());
printInfo(persons);
}
public static void printInfo(List<Person> list) {
for (Person person : list) {
System.out.println(" name = " + person.getName() + ", age = " + person.getAge());
}
}
}
class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
}
class Person {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
结果:
排序之前:
name = name0, age = 80
name = name1, age = 16
name = name2, age = 12
name = name3, age = 22
name = name4, age = 22
排序之后
name = name2, age = 12
name = name1, age = 16
name = name3, age = 22
name = name4, age = 22
name = name0, age = 80
升序,降序
Comparator
升序
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
降序
@Override
public int compare(Person o1, Person o2) {
return o2.getAge() - o1.getAge();
}
Comparable
升序
@Override
public int compareTo(Person o) {
return this.age - o.age;
}
降序
@Override
public int compareTo(Person o) {
return o.age - this.age;
}