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

When to use Comparable vs Comparator

 
阅读更多
I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works. 

Now looking back at this, I am wondering if I should have instead have the my class implement Comparable instead of creating a new class that implements Comparator. The score is the only field that the objects will be ordered on. 
1.
What I have done acceptable as a practice? 

2.
Is the right approach "First have the class implement Comparable (for the natural ordering) and if an alternative field comparison is required, then create a new class that implements Comparator" ?

 3.
If (2) above is true, then does it mean that one should implement Comparator only after they have the class implement Comparable? (Assuming I own the original class).

 
Thanks!

Use Comparable if you want to define a default (natural) ordering behaviour of the object in question, a common practice is to use a technical or natural (database?) identifier of the object for this.

Use Comparator if you want to define an external controllable ordering behaviour, this can override the default ordering behaviour.

I would say that an object should implement Comparable if that is the clear natural way to sort the class, and anyone would need to sort the class would generally want to do it that way.

If, however, the sorting was an unusual use of the class, or the sorting only makes sense for a specific use case, then a Comparator is a better option.

Put another way, given the class name, is it clear how a comparable would sort, or do you have to resort to reading the javadoc? If it is the latter, odds are every future sorting use case would require a comparator, at which point the implementation of comparable may slow down users of the class, not speed them up.

分享到:
评论

相关推荐

    Comparable&Comparator区别

    ### Comparable与Comparator的区别详解 #### 一、引言 在Java编程中,为了对自定义对象进行排序,Java提供了两种接口:`Comparable`与`Comparator`。这两种接口各有优势,适用于不同的场景。本文将深入探讨这两种...

    java中Comparable和Comparator的区别

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

    Java中Comparable和Comparator的区别

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

    Comparable与Comparator的区别

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

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

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

    Comparable和Comparator区分1

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

    Comparable接口和Comparator使用示例

    Comparable 接口和 Comparator 使用示例 在 Java 编程语言中,比较和排序是非常常见的操作。Comparable 接口和 Comparator 是两种常用的比较和排序方式。在本文中,我们将通过实例代码,详细介绍 Comparable 接口和...

    Comparable与Comparator的区别Java开

    1. **自然顺序 vs 自定义顺序**:`Comparable`提供了对象的自然顺序,而`Comparator`允许你在运行时定义自定义的比较逻辑。 2. **实现方式**:`Comparable`是通过类实现接口,而`Comparator`是在排序时创建和传递一...

    详解Java中Comparable和Comparator接口的区别

    Java 中的 Comparable 和 Comparator 接口详解 Java 中的 Comparable 和 Comparator 接口都是用于比较和排序的,但是它们之间存在着一些关键的区别。Comparable 是一个排序接口,若一个类实现了 Comparable 接口,...

    java排序Comparator和Comparable

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

    Java Comparable和Comparator对比详解

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

    java 中Comparable与Comparator详解与比较

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

    JAVA集合的使用(List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类、equals、hashCode)

    要注意的是List,Set,Queue继承了Collection接口,...这里想用一个简单的例子展示一下他们的使用,内容包括:List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类,泛型、重写equals、hashCode

    comparator接口与Comparable接口的区别

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

    ComparatorAndComparable

    教你学会Comparator和Comparable

    比较器(Comparable、Comparator)

    NULL 博文链接:https://chaoyi.iteye.com/blog/2082723

    Java 比较接口comparable与comparator区别解析

    Java 比较接口comparable与comparator区别解析 Java 语言中提供了两个比较接口: Comparable 和 Comparator,这两个接口都是用于比较对象的大小顺序的,但它们有着不同的使用场景和实现方式。在本文中,我们将详细...

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

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

Global site tag (gtag.js) - Google Analytics