当前位置:首页 > 开发 > 开源软件 > 正文

Difference between Comparator and Comparable in Java - Interview Question

发表于: 2013-08-10   作者:cywhoyi   来源:转载   浏览次数:
摘要: 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 fa

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.

Difference between Comparator and Comparable in Java - Interview Question

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
本章目标 掌握Comparable比较接口的使用 了解比较器的基本排序原理 掌握Comparator比较接口的使用 C
相同点:都是用于比较,排序的接口。例如对于List对象,如果存储的对象类型不是自定义的,像String
WCF has introduced lot of bindings and protocols. This article will concentrate on two import
WCF has introduced lot of bindings and protocols. This article will concentrate on two import
Difference between OLTP and OLAP Generally speaking, data in an OLTP database is generally or
frame 是CGRect 类型,frame.origin有x,y属性,frame.size有width,height属性
细心的人会发现当我们new 一个文件的时候会发现下图的部分. 但是这个问题来了Category 和 Extension
细心的人会发现当我们new 一个文件的时候会发现下图的部分. 但是这个问题来了Category 和 Extension
转自 http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/ Introduction A WSDL
The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,heigh
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号