comparable v comparator
2011-10-29 11:12
113 查看
Java sorting - comparable v comparator
In Java, you can sort objects that implement the Collection interface, using either the comparable interface or a comparator class. What's the difference?
A Comparable interfaced class must contain a method called
compareTo to compare two objects (one being the object on which it is called and the other being passed as a paramater) which returns an integer, negative for
a < b, positive for a > b and 0 otherwise. It must also contain an equals method, returning a boolean, on the same parameters.
A Comparator is a class in its own right, which implements the Comparator interface; that means it must contain a method called
compare (two objects as parameters) which returns a negative, zero or positive integer depensing on whether the first object is less than, equal to, or greater than the second.
If you want to sort a collection using its comparable interface, you simply call the static
Collections.sort method on it ... so if we had an ArrayList called Actable which implemented Comparable, we could write:
To sort a collection using a Comparator class, you need to pass an extra parameter into the
Collections.sortmethod - that parameter being an instance of a Comparator object. Thus:
So in effect, using the comparable interface gives you a default sort order of your own making, whereas the
comparator is more flexible as it allows you define a whole series of different types of sorting that can be applied to a whole lot of different types of objects.
(written 2007-02-02, updated 2007-02-05)
In Java, you can sort objects that implement the Collection interface, using either the comparable interface or a comparator class. What's the difference?
A Comparable interfaced class must contain a method called
compareTo to compare two objects (one being the object on which it is called and the other being passed as a paramater) which returns an integer, negative for
a < b, positive for a > b and 0 otherwise. It must also contain an equals method, returning a boolean, on the same parameters.
A Comparator is a class in its own right, which implements the Comparator interface; that means it must contain a method called
compare (two objects as parameters) which returns a negative, zero or positive integer depensing on whether the first object is less than, equal to, or greater than the second.
If you want to sort a collection using its comparable interface, you simply call the static
Collections.sort method on it ... so if we had an ArrayList called Actable which implemented Comparable, we could write:
Collections.sort(Actable);
To sort a collection using a Comparator class, you need to pass an extra parameter into the
Collections.sortmethod - that parameter being an instance of a Comparator object. Thus:
Collections.sort(Actable, new Bynum());
So in effect, using the comparable interface gives you a default sort order of your own making, whereas the
comparator is more flexible as it allows you define a whole series of different types of sorting that can be applied to a whole lot of different types of objects.
(written 2007-02-02, updated 2007-02-05)
相关文章推荐
- Java中的排序 Comparator,Comparable
- Comparable与Comparator
- comparable以及comparator接口比较
- Comparable接口及其与Comparator在排序中的应用
- Comparable与Comparator的区别
- How To Sort Objects With Java Interface Comparable and Comparator
- java的Comparator和Comparable
- Comparable与Comparator接口的区别
- List中的数据如何根据对象的某一个或多个字段排序引出Comparable和comparator的使用
- java学习笔记13--比较器(Comparable、Comparator)
- J2SE基础夯实系列之List中的数据如何根据对象的某一个或多个字段排序引出Comparable和comparator的使用-- Collections与Arrays
- comparable与comparator的区别
- java基础--Comparable和Comparator的区别
- Comparator和Comparable在排序中的应用
- Comparator和Comparable在排序中的应用
- Comparable& Comparator
- Java中Comparable和Comparator实现对象比较
- comparator接口与Comparable接口的区别
- Java:对象排序:Comparator&Comparable
- 黑马程序员-Set-HashSet-TreeSet-Comparator-Comparable