一.java.lang.Comparable 和 java.util.Comparator的共同点
# 都是接口,作用都是对自定义的类附加上可比大小的能力,这样同一实例的多个对象之间就有了“大小”(此处的大小逻辑由自己实现)之分。
自定义的类:比如 public class Person{String name,int age;}
当我们有这么一个personList,里面包含person1,person2,person3...,我们可以用Collections.sort(personList)对personList进行排序,当然前提是需要实现一个比较器。在现实编程 中,我们可能遇到字符串list 如: stringList{"hello1","hello2","hello3"..}, 用 Collotions.sort(stringList)能够得到正确的排序,那是因为String类已经帮我们实现了Comparab接口。
二.Comparable and Comparator 的区别
1.实现Comparable接口的类是在该类的内部通过重写public int compareTo(T t)来实现比较器的,优点是:运用简单,缺点是:需要修改原有的类
2.运用Comparator接口,需要利用第三方类来实现Comparator接口,并通过重写public int compare(Object o1, Object o2) 来实现比较器。优点是:不需要修改原有的类,缺点:运用比较麻烦。
Comparable 实例:
package comparatorVScomparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*通过实现Comparable和重写compareTo(Object o)方法,使得类Student实现比较器,
因此Student类的对象stu1,stu2,st3....自身有了大小之分,所以可以用Collections.sort(...)实现排序,具体的排序规则由被重写的compareTo(Object o)方法决定。
*/
class Student implements Comparable<Student> {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student stu) {
int i = 0;
i = name.compareTo(stu.name);
if (i == 0) {
return this.age - stu.age;
}
return i;
}
}
public class ComparableTest {
public static void main(String[] args) {
List<Student> stuList = new ArrayList<Student>();
Student stu1 = new Student("alii", 1);
Student stu2 = new Student("catt", 2);
Student stu3 = new Student("bill", 5);
Student stu4 = new Student("girl", 3);
Student stu5 = new Student("hitt", 4);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
stuList.add(stu5);
System.out.println("排序前:");
for (Student s : stuList) {
System.out.println(s.name + " " + s.age);
}
// 实现排序
Collections.sort(stuList);
System.out.println("排序后:");
for (Student s : stuList) {
System.out.println(s.name + " " + s.age);
}
}
}
运行结果:
排序前:
alii 1
catt 2
bill 5
girl 3
hitt 4
排序后:
alii 1
bill 5
catt 2
girl 3
hitt 4
Comparator 实例:
package comparatorVScomparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// 在person类外部实现比较器,比较逻辑在compare(object o1,object o2)方法里,不用修改原有的类
class Mysort implements Comparator<Person> {
// 重写compare方法,实现比较逻辑
@Override
public int compare(Person o1, Person o2) {
int i = 0;
i = o1.name.compareTo(o2.name);
if (i == 0) {
return o1.age - o2.age;
}
return i;
}
}
public class ComparatorTest {
public static void main(String[] args) {
List<Person> pList = new ArrayList<Person>();
Person p1 = new Person("alii", 1);
Person p2 = new Person("catt", 2);
Person p3 = new Person("bill", 5);
Person p4 = new Person("girl", 3);
Person p5 = new Person("hitt", 4);
pList.add(p1);
pList.add(p2);
pList.add(p3);
pList.add(p4);
pList.add(p5);
System.out.println("排序前:");
for (Person p : pList) {
System.out.println(p.name + " " + p.age);
}
// 排序
Collections.sort(pList, new Mysort());
System.out.println("排序后:");
for (Person p : pList) {
System.out.println(p.name + " " + p.age);
}
}
}
运行结果:
排序前:
alii 1
catt 2
bill 5
girl 3
hitt 4
排序后:
alii 1
bill 5
catt 2
girl 3
hitt 4