Comparable vs Comparator
- Comparable interface can be used to provide single way of sorting whereas Comparator interface is used to provide different ways of sorting.
- For using Comparable, Class needs to implement it whereas for using Comparator we don’t need to make any change in the class.
- Comparable interface is in java.lang package whereas Comparator interface is present in java.utilpackage.
- We don’t need to make any code changes at client side for using Comparable, Arrays.sort() or Collection.sort() methods automatically uses the compareTo() method of the class. For Comparator, client needs to provide the Comparator class to use in compare() method.

public class Employee implements Comparable<Employee> {
private Long id;
private String name;
private Double salary;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Employee() {
}
public Employee(Long id, String name, Double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
@Override
public int compareTo(Employee o) {
return (int) (this.id - o.id);
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("id", id)
.append("name", name)
.append("salary", salary)
.toString();
}
}
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee(13L, "hello", 8000D));
employeeList.add(new Employee(17L, "world", 10000D));
employeeList.add(new Employee(13L, "test", 12000d));
employeeList.add(new Employee(14L, "demo", 15000D));
Collections.sort(employeeList);
for (Employee e : employeeList) {
System.out.println(e);
}
Collections.sort(employeeList, new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
int flag = (int) (o1.getId() - o2.getId());
if (flag == 0) {
flag = (int) (o2.getSalary() - o1.getSalary());
}
return flag;
}
});
System.out.println("comparator sort:");
for (Employee e : employeeList) {
System.out.println(e);
}
}
Employee@4d76f3f8[id=13,name=hello,salary=8000.0]
Employee@2d8e6db6[id=13,name=test,salary=12000.0]
Employee@23ab930d[id=14,name=demo,salary=15000.0]
Employee@4534b60d[id=17,name=world,salary=10000.0]
comparator sort:
Employee@2d8e6db6[id=13,name=test,salary=12000.0]
Employee@4d76f3f8[id=13,name=hello,salary=8000.0]
Employee@23ab930d[id=14,name=demo,salary=15000.0]
Employee@4534b60d[id=17,name=world,salary=10000.0]
Comparable vs Comparator的更多相关文章
- Java中Comparable与Comparator的区别
相同 Comparable和Comparator都是用来实现对象的比较.排序 要想对象比较.排序,都需要实现Comparable或Comparator接口 Comparable和Comparator都 ...
- 对象比较器:Comparable和Comparator
在进行对象数组排序的过程中需要使用到比较器,比较器有两个:Comparable和Comparator ①.java.lang.Comparable:是在类定义是时候默认实现好的接口,里面提供有一个co ...
- Java中Comparable和Comparator接口区别分析
Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...
- Comparable和Comparator的区别
Comparable Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较 ...
- Java中Comparable和Comparator区别小结
一.Comparable简介 Comparable是排序接口.若一个类实现了Comparable接口,就意味着该类支持排序.实现了Comparable接口的类的对象的列表或数组可以通过Collecti ...
- Java 中 Comparable 和 Comparator 比较
Java 中 Comparable 和 Comparator 比较 目录: Comparable Comparator Comparable 和 Comparator比较 第二个例子 之 Compar ...
- Comparable与Comparator
转载 Comparable与Comparator的区别 (转载) Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部 ...
- Java中的TreeMap、Comparable、Comparator
我们知道HashMap的存储位置是按照key这个对象的hashCode来存放的,而TreeMap则是不是按照hashCode来存放,他是按照实现的Comparable接口的compareTo这个方法来 ...
- 比较器comparable与comparator的使用
在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...
- 简谈Comparable和Comparator区别
对于Comparable和Comparator这连个相似的接口,还是做一下比较比较好: Comparable Comparator (1)只包含一个compareTo()方法,此方法可以给两个对象排序 ...
随机推荐
- 总结Linux下的软件安装
安装软件的最佳实践 虽然我们知道Linux下安装软件有三种方式,分别是源代码安装,rpm包安装和yum安装,但是从可控性和结合自己目前的水平来说,优先选择以下两种方式安装程序. 1,使用rpm包安装 ...
- Html和Css学习笔记-css基础知识
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 此篇博客是我的复习笔记,html和css学的时间太久了,忘得差不多了,最近要使用一下,所以重新打开html的书略读,后记录了标签 ...
- 2019年度SAP项目实践计划
2019年度SAP项目实践计划 一晃2018年过去了,而新的一年说来就来了. 对于新的一年,笔者也难免有所畅想.早在上个月下旬就开始制定新年的诸多计划,比如写作计划,比如人工智能学习计划,比如新年度旅 ...
- Dynamics AX 2012 R2 电子邮件广播错误 0x80040213
Dynamics AX 2012 R2 电子邮件广播错误 0x80040213 今天Reinhard在新环境做邮件广播测试时,发现无法发送邮件,并报以下错误: 类"CDO.Message&q ...
- java新知识系列 一
内联函数: 所谓内联函数就是指函数在被调用的地方直接展开,编译器在调用时不用像一般函数那样,参数压栈,返回时参数出栈以及资源释放等,这样提高了程序执行速度. 对应Java语言中也有一个关键字final ...
- (转)hibernateTools工具安装及使用总结(eclipse 3.6)
最近项目采用flex+spring+hibernate的框架开发,之前虽说有多年的Java开发经验了,但是一直使用的JDBC或者 ibatis,hibernate的使用还是大姑娘上轿头一回,网上都介绍 ...
- c# winform多线程实时更新控件
//创建委托 private delegate void SetTextCallback(string text); /// <summary> / ...
- 关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的几个实例
在推行系统中,时不时会有用户提出希望系统能自动推送邮件,由于手头的工具和能力有限,不少需求都借助于sql server的邮件触发来实现. 步骤: 1.配置邮箱.步骤略,网上有不少帖子说明,手工直接在管 ...
- 我的第一个python web开发框架(25)——定制ORM(一)
在开始编写ORM模块之前,我们需要先对db_helper进行重构,因为ORM最终生成的sql是需要转给db_helper来执行的,所以拥有一个功能完善.健壮的数据库操作类是非常必要的. 这是项目原db ...
- springcloud ribbon 客户端负载均衡用法
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...