Comparable与Comparator的区别
2017-07-30 19:24
369 查看
Comparable & Comparator 都是用来实现集合中元素的比较、排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序,所以,如想实现排序,就需要在集合外定义 Comparator 接口的方法或在集合内实现 Comparable 接口的方法。
Comparator位于包Java.util下,而Comparable位于包 java.lang下
Comparable 是一个对象本身就已经支持自比较所需要实现的接口(如 String、Integer 自己就可以完成比较大小操作,已经实现了Comparable接口)
自定义的类要在加入list容器中后能够排序,可以实现Comparable接口,在用Collections类的sort方法排序时,如果不指定Comparator,那么就以自然顺序排序,如API所说:
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface
这里的自然顺序就是实现Comparable接口设定的排序方式。
而 Comparator 是一个专用的比较器,当这个对象不支持自比较或者自比较函数不能满足你的要求时,你可以写一个比较器来完成两个对象之间大小的比较。
可以说一个是自已完成比较,一个是外部程序实现比较的差别而已。
用 Comparator 是策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。
比如:你想对整数采用绝对值大小来排序,Integer 是不符合要求的,你不需要去修改 Integer 类(实际上你也不能这么做)去改变它的排序行为,只要使用一个实现了 Comparator 接口的对象来实现控制它的排序就行了。
以pat
https://www.patest.cn/contests/pat-a-practise/1028为例,以comprator实现多种条件下的自定义排序
Comparator位于包Java.util下,而Comparable位于包 java.lang下
Comparable 是一个对象本身就已经支持自比较所需要实现的接口(如 String、Integer 自己就可以完成比较大小操作,已经实现了Comparable接口)
自定义的类要在加入list容器中后能够排序,可以实现Comparable接口,在用Collections类的sort方法排序时,如果不指定Comparator,那么就以自然顺序排序,如API所说:
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface
这里的自然顺序就是实现Comparable接口设定的排序方式。
而 Comparator 是一个专用的比较器,当这个对象不支持自比较或者自比较函数不能满足你的要求时,你可以写一个比较器来完成两个对象之间大小的比较。
可以说一个是自已完成比较,一个是外部程序实现比较的差别而已。
用 Comparator 是策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。
比如:你想对整数采用绝对值大小来排序,Integer 是不符合要求的,你不需要去修改 Integer 类(实际上你也不能这么做)去改变它的排序行为,只要使用一个实现了 Comparator 接口的对象来实现控制它的排序就行了。
以pat
https://www.patest.cn/contests/pat-a-practise/1028为例,以comprator实现多种条件下的自定义排序
import java.util.*; public class Main { static class Student { int id; String name; int grade; Student(int id, String name, int grade) { this.id = id; this.name = name; this.grade = grade; } } public static void main(String[] args) { List<Student> list = new ArrayList<Student>(); int n, d; Scanner in = new Scanner(System.in); n = in.nextInt(); d = in.nextInt(); for ( int i = 0; i < n; i++ ) { int id = in.nextInt(); String name = in.next(); int grade = in.nextInt(); list.add(new Student(id, name, grade)); } if ( d == 1 ) { sort(1, list); } else if ( d == 2){ sort(2, list); } else { sort(3, list); } for ( int i = 0; i < n; i++ ) { Student cas = list.get(i); System.out.printf("%06d %s %d", cas.id, cas.name, cas.grade); System.out.println(); } } static void sort(int re, List<Student> list) { if ( re == 1 ) { Collections.sort(list, new comparator1()); } else if ( re == 2 ) { Collections.sort(list, new comparator2()); } else { Collections.sort(list, new comparator3()); } } static class comparator1 implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return (o1.id - o2.id) <= 0 ? -1 : 1; } } static class comparator2 implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub if ( o1.name.equals(o2.name) ) return (o1.id - o2.id) <= 0 ? -1 : 1; return o1.name.compareTo(o2.name); } } static class comparator3 implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub if ( o1.grade == o2.grade ) return (o1.id - o2.id) <= 0 ? -1 : 1; return (o1.grade - o2.grade <= 0) ? -1 : 1; } } }
相关文章推荐
- Comparable 和 Comparator 的区别
- comparable 与comparator的区别?
- 比较java comparable和Comparator 区别
- comparator接口与Comparable接口的区别
- Comparable和Comparator的区别
- Java中comparable和comparator的区别
- Comparable和Comparator的区别?
- Comparable接口和Comparator接口的区别
- Comparable和Comparator的区别
- Comparable 与 Comparator 的区别
- java中Comparable 和 Comparator 区别及实例演示
- comparator和 comparable 区别
- comparator接口与Comparable接口的区别
- Comparator 与 Comparable 的区别
- Comparator和Comparable的区别使用
- comparator接口与Comparable接口的区别
- Java 中Collection和Collections 、Comparable 和Comparator的区别
- Comparable 和 Comparator的区别
- Comparable接口与Comparator接口区别
- Java记录 -69- Comparable与Comparator的区别