java中comparator与comparable的解析
一.comparable与comparator的不同点
1.首先comparable是在java.lang包下的接口,而comparator是java.util包下的接口。
2.类实现comparable接口后需要实现下面的方法:
public int compareTo(T o) {
// TODO Auto-generated method stub
return 0 ;
}
而实现Comparator的类,测需要实现下面的方法:
public int compare(Object o1, Objecto2) {
// TODO Auto-generated method stub
return 0;
}
3.实现comparable的接口的类,它的对象就可以进行自比较,通过Arrays.sort()或Collections.sort()就可以对包含此对象的数组或集合进行排序。看下面例子:
首先,先定义一个Person类,实现Comparable接口,我们以年龄作为排序的依据:
public class Person<T> implements Comparable<T> {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(T o) {
// TODO Auto-generated method stub
return ((Person)o).getAge() - this.age ;
}
}
然后,定义一个测试类,如下:
public class TestCompare {
public static void main(String[] args){
List<Person> list = new ArrayList<Person>();
Person p1 = new Person("a","male",1);
Person p2 = new Person("b","female",2);
Person p3 = new Person("c","male",3);
list.add(p3);
list.add(p1);
list.add(p2);
Collections.sort(list);
for(Person p:list){
System.out.println("Name: " + p.getName() + " "
+ "Sex: " + p.getSex() + " " + "Age: " + p.getAge());
}
}
输出结果为:
Name: c Sex: male Age: 3
Name: b Sex: female Age: 2
Name: a Sex: male Age: 1
这是是降序排列,把compareTo中的方法做如下改动,就变成升序排列了。
public int compareTo(T o) {
// TODO Auto-generated method stub
return this.age - ((Person)o).getAge() ;
}
4.有时类并没有实现comparable接口,而我们又需要对包含此类对象的数组或集合进行排序,那么我们就需要实现Comparator这个比较器了。新建一个person类:
public class Person<T> {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
再定义一个测试类:
public class TestCompare {
public static void main(String[] args){
List<Person> list = new ArrayList<Person>();
Person p1 = new Person("a","male",1);
Person p2 = new Person("b","female",2);
Person p3 = new Person("c","male",3);
list.add(p3);
list.add(p1);
list.add(p2);
Collections.sort(list , emCpmpare);
for(Person p:list){
System.out.println("Name: " + p.getName() + " "
+ "Sex: " + p.getSex() + " " + "Age: " + p.getAge());
}
}
public static Comparator<Person> emCpmpare = new Comparator<Person>(){
@Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
String name1 = ((Person)o1).getName();
String name2 = ((Person)o2).getName();
if(!name1.isEmpty() && !name2.isEmpty())
return name1.compareTo(name2);
else {
int age1 = ((Person)o1).getAge();
int age2 = ((Person)o2).getAge();
return age1 - age2;
}
}
};
}
这样就可以实现person的排序了。