`
iloveflower
  • 浏览: 79734 次
社区版块
存档分类
最新评论
  • iloveflower: 呵呵。好好学习。。。。。。。。。。。。
    java 读书
  • Eric.Yan: 看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
    java 读书

How to use Comparator and Comparable in Java? With example

 
阅读更多
Read more: http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html#ixzz1uaFRiHo5

What is Difference between Comparator and Comparable in Java question was asked in a Test paper for one of big Investment bank first round of interview. It was not that straight forward but it was related to how you will sort Employee object based on his EmployeeID and his name and that involves the use of both Comparable and Comparator in Java.

Comparators and comparable in Java are two of fundamental interface of Java API which is very important to understand to implement sorting in Java. It’s often required to sort objects stored in any collection class or in Array and that time we need to use compare () and compare To () method defined in java.util.Comparator and java.lang.Comparable class. Let’s see some important points about both Comparable and Comparator in Java before moving ahead

Difference between Comparator and Comparable in Java
1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package.

2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.

4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.

5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using  Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.

6)Objects which implement Comparable in Java  can be used as keys in a sorted map or elements in a sorted set for example TreeSet, without specifying any Comparator.


Example of using Comparator and Comparable in Java

So in Summary if you want to sort based on natural order or object then use Comparable in Java and if you want to sort on some other attribute of object then Comparator in Java  is the way to go. Now to understand these concepts lets see an example

1) There is class called Person, sort the Person based on person_id.
2) Sort the Person based on Name.

For a Person class sorting based on person_id can be treated as natural order sorting and sorting based on Name can be implemented using Comparator interface. To sort based on person_id we need to implement compareTo() method.


public class Person implements Comparable {
    private int person_id;
    private String name;
/**
     * Compare current person with specified person
     * return zero if person_id for both person is same
     * return negative if current person_id is less than specified one
      * return positive if specified person_id is greater than specified one
     */
    public int compareTo(Person o) {
        return this.person_id - o.person_id ;
    }
    ….
}

And for sorting based on person name we can implement compare (Object o1, Object o2) method of Comparator in Java or Java Comparator class.

public class PersonSortByPerson_ID implements Comparator{

     public int compare(Person o1, Person o2) {
        return o1.getPersonId() - o2.getPersonId();
    }
}

You can write several types of Java Comparator based upon your need for example  reverseComparator , ANDComparator , ORComparator etc which will return negative or positive number based upon logical results.

分享到:
评论

相关推荐

    ComparatorAndComparable

    教你学会Comparator和Comparable

    java排序Comparator和Comparable

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

    java的Comparator和Comparable.docx

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

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

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

    comparator接口与Comparable接口的区别

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

    java中Comparable和Comparator的区别

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

    Comparable&Comparator区别

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

    Comparable与Comparator的区别Java开

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

    Comparable接口和Comparator使用示例

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

    Java中Comparable和Comparator的区别

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

    Java中Comparable和Comparator 2种实现方式学习

    在Java编程语言中,排序是常见的操作,而`Comparable`和`Comparator`接口则是实现对象排序的两种主要方式。这篇文章将深入探讨这两种实现方式及其在实际编程中的应用。 首先,`Comparable`接口位于`java.lang`包下...

    java Comparator 用法 例子

    Java中的Comparator接口是排序的关键工具,它允许程序员自定义对象的比较规则,从而在集合、数组或树结构(如TreeSet、TreeMap)中实现定制化的排序。在Java的Collections框架中,Comparator扮演着重要的角色,特别...

    java 中Comparable与Comparator详解与比较

    在Java编程语言中,Comparable和Comparator接口用于比较对象的顺序,尤其在进行排序操作时非常关键。两者虽然目的相似,但使用方式和应用场景有所区别。 Comparable接口定义在`java.lang`包中,它只有一个方法`...

    Java Comparable和Comparator对比详解

    Java Comparable和Comparator对比详解 Java 中的 Comparable 和 Comparator 是两个常用的接口,用于实现对象的比较和排序。它们都是 Java 中的接口,都是用于比较对象的大小的,但它们有所不同。 Comparable 接口 ...

    Comparable与Comparator的区别

    在Java编程语言中,Comparable和Comparator是两个非常重要的接口,它们都用于对象的比较和排序。下面是关于这两个接口的详细解释: **Comparable接口** Comparable接口位于`java.lang`包下,它是一个对象自比较的...

    Comparable和Comparator区分1

    【Comparable和Comparator区分详解】 Comparable接口是Java中用于对象排序的关键接口,主要应用于需要自动排序的场景。当一个类实现了Comparable接口,意味着该类的对象具备内在的比较逻辑,可以直接进行比较。例如...

    class MyComparator implements Comparator类的算法 java

    `Comparator`可以用于任何实现了`Comparable`接口的类,或者当我们想要对不支持自然排序的对象进行排序时。标题提到的`MyComparator`就是一个自定义的比较器类,它实现了`Comparator`接口,用于特定的算法排序。 `...

Global site tag (gtag.js) - Google Analytics