一、概述
Comparable和Comparator都是用来实现集合中元素的比较、排序的。
Comparable是在集合内部定义的方法实现的排序,位于java.lang下。
Comparator是在集合外部实现的排序,位于java.util下。
Comparable是一个对象本身就已经支持自比较所需要实现的接口,如String、Integer自己就实现了Comparable接口,可完成比较大小操作。自定义类要在加入list容器中后能够排序,也可以实现Comparable接口,在用Collections类的sort方法排序时若不指定Comparator,那就以自然顺序排序。所谓自然顺序就是实现Comparable接口设定的排序方式。
Comparator是一个专用的比较器,当这个对象不支持自比较或者自比较函数不能满足要求时,可写一个比较器来完成两个对象之间大小的比较。Comparator体现了一种策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。
总而言之Comparable是自已完成比较,Comparator是外部程序实现比较。
二、例子
①Comparator的绝对值比较器
若不是调用sort方法,相要直接比较两个对象的大小,Comparator定义了俩个方法,分别是int compare(T o1,T o2)和boolean equals(Object obj)。有时在实现Comparator接口时,并没有实现equals方法,可程序并没有报错,原因是实现该接口的类也是Object类的子类,而Object类已经实现了equals方法。
Comparable和Comparator都是用来实现集合中元素的比较、排序的。
Comparable是在集合内部定义的方法实现的排序,位于java.lang下。
Comparator是在集合外部实现的排序,位于java.util下。
Comparable是一个对象本身就已经支持自比较所需要实现的接口,如String、Integer自己就实现了Comparable接口,可完成比较大小操作。自定义类要在加入list容器中后能够排序,也可以实现Comparable接口,在用Collections类的sort方法排序时若不指定Comparator,那就以自然顺序排序。所谓自然顺序就是实现Comparable接口设定的排序方式。
Comparator是一个专用的比较器,当这个对象不支持自比较或者自比较函数不能满足要求时,可写一个比较器来完成两个对象之间大小的比较。Comparator体现了一种策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。
总而言之Comparable是自已完成比较,Comparator是外部程序实现比较。
二、例子
①Comparator的绝对值比较器
public class AbsComparator<T> implements Comparator<T> { public int compare(Object o1, Object o2) { int ovalue1 = Math.abs(((Integer) o1).intValue()); int ovalue2 = Math.abs(((Integer) o2).intValue()); return (ovalue1 > ovalue2) ? 1 : (ovalue1 == ovalue2 ? 0 : -1); } } import java.util.Arrays; import java.util.Random; public class AbsComparatorTest { public static void main(String[] args) { // 使用方法1 Random rn = new Random(); Integer[] integerArray = new Integer[20]; for (int i = 0; i < integerArray.length; i++) { integerArray[i] = new Integer(rn.nextInt(100) * (rn.nextBoolean() ? 1 : -1)); } System.out.println("用Integer内置方法排序:"); Arrays.sort(integerArray); System.out.println(Arrays.asList(integerArray)); System.out.println("用AbsComparator排序:"); Arrays.sort(integerArray, new AbsComparator<Integer>()); System.out.println(Arrays.asList(integerArray)); // 使用方法2 System.out.println("用AbsComparator比较-100和10的绝对值大小结果是:"); AbsComparator<Integer> absComparator = new AbsComparator<Integer>(); int result = absComparator.compare(new Integer(-100), new Integer(10)); System.out.println(result); } }
若不是调用sort方法,相要直接比较两个对象的大小,Comparator定义了俩个方法,分别是int compare(T o1,T o2)和boolean equals(Object obj)。有时在实现Comparator接口时,并没有实现equals方法,可程序并没有报错,原因是实现该接口的类也是Object类的子类,而Object类已经实现了equals方法。
②comparable和comparator综合例子
// 实现Comparable接口 public class Person implements Comparable<Object> { private int num; private String name; private int age; public Person(int num, String name, int age) { super(); this.num = num; this.name = name; this.age = age; } public int compareTo(Object o) { return this.age - ((Person) o).getAge(); } public int getNum() { return num; } public void setNum(int num) { this.num = num; } 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; } } // 实现Comparator接口 public class PersonComparator implements Comparator<Object> { @Override public int compare(Object o1, Object o2) { int num1 = ((Person) o1).getNum(); int num2 = ((Person) o2).getNum(); return num1 > num2 ? 1 : (num1 == num2 ? 0 : -1); } } // 测试 public class PersonTest { public static void main(String[] args) { Person p1 = new Person(1, "xy1", 22); Person p2 = new Person(2, "xy1", 21); System.out.println("实现Comparable接口方法结果(按年龄比较):"); // 结果为1 System.out.println(p1.compareTo(p2)); System.out.println("实现Comparator接口方法结果(按学号比较):"); // 结果为-1 PersonComparator pc = new PersonComparator(); System.out.println(pc.compare(p1, p2)); // 也可用声明一个Person[] ps,通过Array.sort(ps)进行排序 } }原帖地址:http://ctzlylc.blog.163.com/blog/static/61967136201165981283/