`
cywhoyi
  • 浏览: 421110 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Difference between Comparator and Comparable in Java - Interview Question

 
阅读更多

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.

分享到:
评论

相关推荐

    Java-中的-Comparator-和-Comparable.md

    Java-中的-Comparator-和-Comparable.md

    ComparatorAndComparable

    教你学会Comparator和Comparable

    Comparator的用法---马克-to-win java视频

    Comparator的用法---马克-to-win java视频的详细描述与介绍

    java排序Comparator和Comparable

    在Java编程语言中,排序是数据处理中一个非常常见的需求,而`Comparator`和`Comparable`接口则是实现排序的关键工具。这两个接口都是用于比较对象,但它们的应用场景和使用方式有所不同。 首先,`Comparable`接口是...

    java的Comparator和Comparable.docx

    在 Java 中,Comparator 和 Comparable 是两个非常重要的接口,它们用于对象的排序操作。这两个接口都是在 java.util 包中定义的,主要用于定制排序规则。 **一、Comparator 接口** Comparator 接口允许我们创建...

    comparator接口与Comparable接口的区别

    Comparator接口与Comparable接口是Java语言中两个重要的接口,它们都是用于比较和排序自定义类的对象的大小的。虽然它们两个都是用于比较的接口,但是它们有着不同的实现方式和应用场景。 相同之处:Comparator接口...

    java中Comparable和Comparator的区别

    在Java编程语言中,Comparable和Comparator接口是两个重要的概念,它们都用于对象的排序,但有着不同的使用场景和特点。本文将深入探讨这两个接口的区别,以及它们在实际开发中的应用。 首先,我们来了解一下...

    JAVA-V2-Interfaces-Comparable-y-Comparator

    在Java编程语言中,`Comparable`和`Comparator`接口是两个非常重要的概念,它们主要用于对象的排序和比较。本文将详细解析这两个接口的工作原理、使用场景以及如何在实际开发中应用。 首先,`Comparable`接口位于`...

    php-sebastian-comparator3-3.0.2-1.el7.remi.noarch.rpm

    php-sebastian-comparator3-3.0.2-1.el7.remi.noarch.rpm

    Comparable&Comparator区别

    在Java编程中,为了对自定义对象进行排序,Java提供了两种接口:`Comparable`与`Comparator`。这两种接口各有优势,适用于不同的场景。本文将深入探讨这两种接口的区别及其应用场景,帮助读者更好地理解它们的工作...

    计算机后端-Java-Java核心基础-第22章 常用类 23. 使用Comparator实现定制排序.avi

    计算机后端-Java-Java核心基础-第22章 常用类 23. 使用Comparator实现定制排序.avi

    Comparable与Comparator的区别Java开

    在Java编程语言中,`Comparable`接口和`Comparator`接口是两种重要的排序机制,它们用于对集合中的对象进行比较和排序。理解它们的区别对于任何Java开发者来说都是至关重要的。 首先,`Comparable`接口位于`java....

    Comparable接口和Comparator使用示例

    Comparable 接口和 Comparator 使用示例 在 Java 编程语言中,比较和排序是非常...Comparable 接口和 Comparator 都是 Java 中非常重要的比较和排序机制,它们提供了不同的比较和排序方式,分别适用于不同的应用场景。

    java-jdk1.8-windows

    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

    python库。 资源全名:number_comparator-0.0.3b1-py3-none-any.whl

    Java中Comparable和Comparator的区别

    在Java编程语言中,Comparable和Comparator接口是两个非常重要的组件,它们主要用来进行对象的比较和排序。了解它们之间的区别对于提升代码的可维护性和灵活性至关重要。 Comparable接口来源于java.lang包,它定义...

    Crazy-JAVA-mind-map.zip_Crazy JAVA mind map_crazy_java-mindmap_m

    同时,接口(如Comparator、Iterable)和设计模式(如工厂模式、单例模式)也是高级Java开发者需要掌握的内容。 文件和I/O流是Java进行数据读写的关键,包括文件操作、字节流、字符流以及缓冲流等。网络编程部分则...

    java-hbase开发包

    Java-HBase 开发包是专为Java开发者设计的,用于与HBase数据库进行交互的工具集。HBase是一个分布式、可扩展的列式存储系统,是Apache Hadoop生态系统的一部分,适用于处理大规模数据。这个开发包使得Java程序员可以...

Global site tag (gtag.js) - Google Analytics