During interviews, you can face this question differently, if you are giving telephonic round than it's mostly fact based i.e. you need to mention key points about both interfaces, while in face to face interviews or during written test, you might be ask to write code to sort objects using Comparator and Comparable e.g. sorting employee by name or branch. I have already discussed second part of this question here and we will only see fact based differences in this post.
1. Comparator interface is in java.util package, which implies it's a utility class, while Comparable interface is kept on java.lang package, which means it's essential for Java objects.
2. Based on syntax, one difference between Comparable and Comparator in Java is that former gives uscompareTo(Object toCompare), which accepts an object, which now uses Generics from Java 1.5 onwards, whileComparator defines compare(Object obj1, Object obj2) method for comparing two object.
3. Continuing from previous difference between Comparator vs Comparable, former is used to compare current object, represented by this keyword, with another object, while Comparator compares two arbitrary object passed to compare()method in Java.
4. One of the key difference between Comparator and Comparable interface in Java is that, You can only have onecompareTo() implementation for an object, while you can define multiple Comparator for comparing objects on different parameters e.g. for an Employee object, you can use compareTo() method to compare Employees on id, known asnatural ordering, while multiple compare() method to order employee on age, salary, name and city. It's also a best practice to declare Comparator as nested static classes in Java, because they are closely associated with objects they compare.
5. Many Java classes, which make uses of Comparator and Comparable defaults to Comparable and provided overloaded method to work with arbitrary Comparator instance e.g. Collections.sort() method, which is used tosort Collection in Java has two implementation, one which sort object based on natural order i.e. by usingjava.lang.Comparable and other which accepts an implementation of java.util.Comparator interface.
6. One more key thing, which is not a difference but worth remembering is that both compareTo() and compare()method in Java must be consistent with equals() implementation i.e. if two methods are equal by equals() method thancompareTo() and compare() must return zero. Failing to adhere this guideline, your object may break invariants of Java collection classes which rely on compare() or compareTo() e.g. TreeSet and TreeMap.
That's all on difference between Comparator and Comparable in Java. Always remember thatjava.lang.Comparable is used to define natural ordering of an object, while java.util.Comparator can be used to define any ordering based upon your requirements. You can define only one ordering with Comparable but can have multipleComparators. I strongly suggest to look my example of comparator vs comparable to gain more insight on how to use really use it in code. From Java interview point of view, I think, you are good to go if you know this much details.
- 浏览: 412535 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (216)
- JAVA (136)
- Python (7)
- 娱乐生活 (10)
- Cloud (5)
- Linux (9)
- JavaScript (7)
- Oracle (11)
- Groovy (1)
- Mysql (3)
- NoSQL (4)
- tomcat (6)
- apache/nginx (16)
- C/C++ (5)
- 设计模式 (7)
- 架构 (8)
- openstack (1)
- hadoop (2)
- 数据仓库 (7)
- linkedin (17)
- JavaGeeker (54)
- Google (9)
- opensource (44)
- spring (21)
- Performance (39)
- lua (2)
- Rust (1)
- WASM (0)
- Golang (0)
- Microservice (1)
- Blockchain (0)
- Web3 (0)
- 边缘计算 (0)
- 硅谷 (0)
- 密码学 (0)
- BTC (0)
- Ethereum (0)
最新评论
-
u012916287:
请问大神。这个架构是你们公司的产品吗?最近准备搞一个基于spr ...
基于DDD的微服务架构设计 -
铁柱他哥:
能给我一份源码码,我这儿边出了点儿问题我参考一下
Jersey采用JSP模板 -
hch2012:
大神,膜拜! ...
基于DDD的微服务架构设计 -
cywhoyi:
lionld23 写道等着看呢,速度速度我是抽空再写的,整篇文 ...
从demo到支持高并发 -
lionld23:
等着看呢,速度速度
从demo到支持高并发
Difference between Comparator and Comparable in Java - Interview Question
- 博客分类:
- JAVA
- JavaGeeker
发表评论
-
Ethereum Solidity Dapp Game Contract
2019-07-30 17:22 477pragma solidity >=0.4.17 &l ... -
Golang实现以太坊转账
2019-04-28 14:07 1405client, err := ethclient.Dia ... -
支持MapDB的Eventstore
2017-04-11 18:15 1471背景:现在微服务大行其道,虽然微服务开山鼻祖是不建议在微服 ... -
类文件结构解析
2017-03-30 15:39 2011就很好奇反编译出来自己写的代码,这里主要使用了五个工具,i ... -
使用Spring-Cloud搭建微服务架构
2017-02-16 14:00 6016搭建一套微服务架构的,我个人觉得必须如下模块: con ... -
电路熔断器(Circuit Breaker)自我思考
2017-01-07 21:20 2088作者:美团点评技术团 ... -
DDD分布式架构设计的BASE一致性
2016-08-09 16:32 2418问题背景: 在DDD的架构设计中最难以解决的 ... -
支持分布式的callback
2016-08-03 13:10 1057项目背景: 之前在某次培训的分享中,谈到r ... -
基于DDD的微服务架构设计
2016-07-20 17:19 11638DDD领域驱动设计(DDD:Domain-Driven D ... -
使用netty完成proxy-server
2016-06-13 16:34 0项目背景: 现在主流的android与back ... -
从demo到支持高并发
2016-02-25 18:16 3388前言: Java语言作用很大,因有众多分门杂类的 ... -
Vert.x3支持JWT
2015-12-28 20:48 3322知识背景: ... -
MapDB的spring整合使用
2015-12-15 23:26 5020MapDB是一个快速、易用 ... -
MapDB与Spring整合使用
2015-12-14 22:58 27MapDB是一个快速、易 ... -
MapDB与Spring整合使用
2015-12-14 22:56 2MapDB是一个快速、易用的嵌入式Java数据库引擎,它提 ... -
MapDB与Spring整合使用
2015-12-14 22:55 2MapDB是一个快速、易用的嵌入式Java数据库引擎,它提 ... -
基于Spring支持JMX
2015-11-08 00:43 3247前言: 基于Vert写的组件,其中在使用过程 ... -
Vertx与Spring配合完成DML操作
2015-10-29 14:26 11155前言 vertx相较于Tomcat不同之处 引用osc ... -
基于Netty4网站架构
2015-10-28 10:44 11757前言: 本人所在公司在业内比较知名的公司, ... -
异常处理
2015-10-08 16:12 2723首先提倡下Hibernate的验证器,功能强大且使用方面,H ...
相关推荐
Java-中的-Comparator-和-Comparable.md
教你学会Comparator和Comparable
java排序Comparator和Comparable
Comparator的用法---马克-to-win java视频的详细描述与介绍
java的Comparator和Comparable
JAVA-V2-Interfaces-Comparable-y-Comparator
本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明
comparator接口与Comparable接口的区别
php-sebastian-comparator3-3.0.2-1.el7.remi.noarch.rpm
计算机后端-Java-Java核心基础-第22章 常用类 23. 使用Comparator实现定制排序.avi
NULL 博文链接:https://hellogava.iteye.com/blog/1570101
Comparable与Comparator的区别Java开发Java经验技巧共3页.pdf.zip
java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子
python库。 资源全名:number_comparator-0.0.3b1-py3-none-any.whl
Comparable&Comparator区别,看完就明白了
Java中Comparable和Comparator的区别共3页.pdf.zip
主要介绍了java 中Comparable与Comparator详解与比较的相关资料,需要的朋友可以参考下