可比和比较器之间的区别? [英] Difference between Comparable and Comparator?

查看:60
本文介绍了可比和比较器之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,Comparator可以比较来自不同类的多个对象,而Comparable只能将一个onject与同一类中的另一个实例进行比较.

My understand is Comparator can compare multiple objects from different classes while Comparable can only compare one onject with another instance in the same class.

比较(对象1)Compare(object1,object2)

Compareto (object1) Compare(object1, object2)

上面的解释是真的吗?

推荐答案

不是真的.
Comparable Comparator 是通用接口,允许比较通用(包括子类)中定义的类型的实例.

Not really.
Comparable and Comparator are generic interfaces that allow to compare instances of the type defined in the generic (subclasses included).

它们之间的主要区别是 Comparable 是直接在要比较对象的类中实现的.

The main difference between them is that Comparable is directly implemented in the class which you want to compare objects.

因此,如果您有一种比较类中实例的方法,即对它们具有自然顺序,则 Comparable 是正确的方法.
另一方面,如果您有多种方法来比较类中的实例,则 Comparable 是不够的.
您应该改用 Comparator s(如果它不存在自然顺序)或同时使用两者(如果它存在自然顺序和某些其他种类的顺序).

Consequently, if you have a single way to compare instances from a class, that is that you have a natural order for them, Comparable is the right approach.
On the other hand, if you have multiple ways to compare instances from a class, Comparable is not enough.
You should use Comparators instead (if it doesn't exist a natural order) or use both (if it exists a natural order and some other kinds of order).

Comparable 之外, Comparator 可能有用的示例:

Example where Comparator can be useful in addition to Comparable :

String 类通过按字典顺序比较两个字符串来实现 Comparable .假设您需要根据不同的规则(它们的长度)对 String List 进行排序.
您将需要定义实现此规则的 Comparator< String> ,例如:

The String class implements Comparable by comparing two strings lexicographically. Suppose you need to sort a List of String according to a different rule : their length.
You will need to define a Comparator<String> that implements this rule such as :

public class StringLengthComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        return Integer.compare(o1.length(), o2.length());
    }

}

现在,您可以使用其自然顺序(使用 Comparable )对 String 进行排序:

Now you could sort Strings by using their natural order (using Comparable) :

List<String> strings = new ArrayList<>();
...
strings.sort();

但是您也可以使用特定的 Comparator< String> :

But you could also use a specific Comparator<String> :

strings.sort(new StringLengthComparator());

或者不使用lambda创建任何类:

Or without creating any class with a lambda:

strings.sort((o1,o2)->Integer.compare(o1.length(), o2.length()));


应使用 Comparator 代替 Comparable 的示例:


Example where Comparator should be used instead of Comparable :

假设您有一个表示银行帐户的 Account 类.
从功能上讲,您没有自然的顺序来对它们进行排序,而是根据客户的需求有多个订单.使该类实现 Comparable 毫无意义.但是创建不同的 Comparator< Account> 将会.

Suppose you have an Account class that represents a bank account.
Functionally you don't have a natural order to sort them but you have instead multiple orders according to the client needs. Making the class to implement Comparable would not make sense. But creating distinct Comparator<Account> would.

只能使用 Comparator 的情况:

Case where only Comparator can be used :

如果要为无法更改源代码(JDK类或第三方类)的类的实例定义顺序,则可以使用 Comparator .

If you want to define a order for instances of a class which you cannot change the source code (JDK class or third party class), Comparator is the way to follow.

这篇关于可比和比较器之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆