Comparable和Comparator的差别
原文地址:http://leihuang.org/2014/11/16/Comparable-Vs-Comparator/
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)来改变它的行为。
Comparable interface
比如我们定义一个类Country,我们希望他的对象有大小关系,且根据countryId来排序的。所以我们让Country实现Comparable接口,使其具备大小关系的能力,代码例如以下:
class Country implements Comparable{
private int countryId ;
@Override
public int compareTo(Object o) {
Country country = (Country)o ;
return this.getCountryId()>country.getCountryId()?1:this.getCountryId()==
country.getCountryId()?0:-1 ;
}
}
此时Country就具备了比較的能力了,如同Integer,String类一样,此时Country对象的集合就能够通过Collections.sort(),或者Arrays.sort(),来进行排序了。
以下我们来看看Comparator使怎样实现的。
Comparator interface
就像我们之前说的一样,Comparator事实上就好像一个工具,它不是使得Country具有排序性。而是他不改变Country类本身,而是单独实现一个排序工具。来提供给Country用。以下我们就来实现这个排序工具类。
class Country{
private int id ;
public Country(int id){
this.id = id ;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class CountryByIdComparator implements Comparator<Country>{
@Override
public int compare(Country o1, Country o2) {
return o1.getId()>o2.getId()?1:o1.getId()==o2.getId()?0:-1 ;
}
}
大家注意到没。单单看Country类。事实上并没有跟比較有不论什么关系,可是它却能通过实现Comparator借口的工具类ContryByIdComparator来实现自己主动排序。此时调用的Collections.sort()就须要两个參数了,一个是集合,一个是实现Comparator接口的工具类。
我们也能够利用匿名内部类来实现,而不须要单独定义ContryByIdComparator这样一个工具类,代码例如以下:
Collections.sort(cityList,new Comparator<City>(){
@Override
public int compare(City o1, City o2) {
return o1.getId()>o2.getId()?1:o1.getId()==o2.getId()?0:-1 ;
} }) ;
Comparator 是策略模式(strategy design pattern),就是不改变对象自身。而用一个策略对象(strategy object)来改变它的行为。
以下这张图就是它们两的差别:
Refference
2014-11-16 17:27:25
Brave,Happy,Thanksgiving !
最新文章
- 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
- NOIP2002矩形覆盖[几何DFS]
- 已解决:Strict Standards: Non-static method cls_image::gd_version() should not be called statically in...
- 局域网通过ip查mac地址、通过mac地址查ip方法
- (18)odoo规范
- php判断http头还是https头
- android SDK和ADT的更新
- windows文件快速搜索软件推荐
- 认识和选用常用的几种 GPRS 模块(转)
- iOS真机测试友盟碰到错误linker command failed with exit code 1 (use -v to see invocation) 百度地图的检索失败 sqlite 错误码
- Spring AOP 通过order来指定顺序
- springboot-yml内list、map组合写法
- vue根据路由变换,切换导航栏样式
- POJ--1328 Radar Installation(贪心 排序)
- 转载的web server实例
- Elastic Job入门(3) - 集成Springboot
- 【C语言】 二叉树的基本运算
- 查看Windows系统里的进程已运行的时间
- ManageEngine卓豪 IT管理峰会圆满结束
- 案例导入和导出Scott用户
热门文章
- Linux学习(二十)软件安装与卸载(三)源码包安装
- WIN7+wampserver2.4+zend stadio10.6.1配置Xdebug
- elasticsearch地理空间操作简单操作
- C# 使用正则表达式去掉字符串中的数字
- linq中group by
- [转载] 基于Redis实现分布式消息队列
- Python之Queue模块
- SHA1 安全哈希算法(Secure Hash Algorithm)
- JavaScript数据可视化编程学习(二)Flotr2,雷达图
- Python [习题] 字典扁平化