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.
- 浏览: 420206 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (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 542pragma solidity >=0.4.17 &l ... -
Golang实现以太坊转账
2019-04-28 14:07 1405client, err := ethclient.Dia ... -
支持MapDB的Eventstore
2017-04-11 18:15 1527背景:现在微服务大行其道,虽然微服务开山鼻祖是不建议在微服 ... -
类文件结构解析
2017-03-30 15:39 2073就很好奇反编译出来自己写的代码,这里主要使用了五个工具,i ... -
使用Spring-Cloud搭建微服务架构
2017-02-16 14:00 6068搭建一套微服务架构的,我个人觉得必须如下模块: con ... -
电路熔断器(Circuit Breaker)自我思考
2017-01-07 21:20 2139作者:美团点评技术团 ... -
DDD分布式架构设计的BASE一致性
2016-08-09 16:32 2466问题背景: 在DDD的架构设计中最难以解决的 ... -
支持分布式的callback
2016-08-03 13:10 1114项目背景: 之前在某次培训的分享中,谈到r ... -
基于DDD的微服务架构设计
2016-07-20 17:19 11739DDD领域驱动设计(DDD:Domain-Driven D ... -
使用netty完成proxy-server
2016-06-13 16:34 0项目背景: 现在主流的android与back ... -
从demo到支持高并发
2016-02-25 18:16 3453前言: Java语言作用很大,因有众多分门杂类的 ... -
Vert.x3支持JWT
2015-12-28 20:48 3376知识背景: ... -
MapDB的spring整合使用
2015-12-15 23:26 5074MapDB是一个快速、易用 ... -
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 3296前言: 基于Vert写的组件,其中在使用过程 ... -
Vertx与Spring配合完成DML操作
2015-10-29 14:26 11216前言 vertx相较于Tomcat不同之处 引用osc ... -
基于Netty4网站架构
2015-10-28 10:44 11810前言: 本人所在公司在业内比较知名的公司, ... -
异常处理
2015-10-08 16:12 2770首先提倡下Hibernate的验证器,功能强大且使用方面,H ...
相关推荐
Java-中的-Comparator-和-Comparable.md
教你学会Comparator和Comparable
Comparator的用法---马克-to-win java视频的详细描述与介绍
在Java编程语言中,排序是数据处理中一个非常常见的需求,而`Comparator`和`Comparable`接口则是实现排序的关键工具。这两个接口都是用于比较对象,但它们的应用场景和使用方式有所不同。 首先,`Comparable`接口是...
在 Java 中,Comparator 和 Comparable 是两个非常重要的接口,它们用于对象的排序操作。这两个接口都是在 java.util 包中定义的,主要用于定制排序规则。 **一、Comparator 接口** Comparator 接口允许我们创建...
Comparator接口与Comparable接口是Java语言中两个重要的接口,它们都是用于比较和排序自定义类的对象的大小的。虽然它们两个都是用于比较的接口,但是它们有着不同的实现方式和应用场景。 相同之处:Comparator接口...
在Java编程语言中,Comparable和Comparator接口是两个重要的概念,它们都用于对象的排序,但有着不同的使用场景和特点。本文将深入探讨这两个接口的区别,以及它们在实际开发中的应用。 首先,我们来了解一下...
在Java编程语言中,`Comparable`和`Comparator`接口是两个非常重要的概念,它们主要用于对象的排序和比较。本文将详细解析这两个接口的工作原理、使用场景以及如何在实际开发中应用。 首先,`Comparable`接口位于`...
php-sebastian-comparator3-3.0.2-1.el7.remi.noarch.rpm
在Java编程中,为了对自定义对象进行排序,Java提供了两种接口:`Comparable`与`Comparator`。这两种接口各有优势,适用于不同的场景。本文将深入探讨这两种接口的区别及其应用场景,帮助读者更好地理解它们的工作...
计算机后端-Java-Java核心基础-第22章 常用类 23. 使用Comparator实现定制排序.avi
在Java编程语言中,`Comparable`接口和`Comparator`接口是两种重要的排序机制,它们用于对集合中的对象进行比较和排序。理解它们的区别对于任何Java开发者来说都是至关重要的。 首先,`Comparable`接口位于`java....
Comparable 接口和 Comparator 使用示例 在 Java 编程语言中,比较和排序是非常...Comparable 接口和 Comparator 都是 Java 中非常重要的比较和排序机制,它们提供了不同的比较和排序方式,分别适用于不同的应用场景。
JDK(Java Development Kit)是开发和运行Java应用程序的基础,它包含Java编译器、Java运行环境、类库以及各种工具,使得开发者能够编写、调试和部署Java应用。 Java JDK 1.8,也称为Java SE 8(Java Standard ...
python库。 资源全名:number_comparator-0.0.3b1-py3-none-any.whl
在Java编程语言中,Comparable和Comparator接口是两个非常重要的组件,它们主要用来进行对象的比较和排序。了解它们之间的区别对于提升代码的可维护性和灵活性至关重要。 Comparable接口来源于java.lang包,它定义...
同时,接口(如Comparator、Iterable)和设计模式(如工厂模式、单例模式)也是高级Java开发者需要掌握的内容。 文件和I/O流是Java进行数据读写的关键,包括文件操作、字节流、字符流以及缓冲流等。网络编程部分则...
Java-HBase 开发包是专为Java开发者设计的,用于与HBase数据库进行交互的工具集。HBase是一个分布式、可扩展的列式存储系统,是Apache Hadoop生态系统的一部分,适用于处理大规模数据。这个开发包使得Java程序员可以...