Java 实现自定义 compareTo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18757805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:48:13  来源:igfitidea点击:

Implementing custom compareTo

javacompareto

提问by Anjan Baradwaj

@Override
public int compareTo(Object t) 
{
    if(t instanceof Student)
    {
        Student s = (Student)t;
        return (this.name.compareTo(s.name));
    }
    else
        return -1;
}

This is my compareTomethod implementation for comparing two Studentobjects based on their name. Is it possible to compare two such objects based on multiple fields i.e., both name and age?

这是我根据名称compareTo比较两个Student对象的方法实现。是否可以基于多个字段(即姓名和年龄)比较两个这样的对象?

采纳答案by Prasad Kharkar

Yes it is possible to compare two objects based on different sort sequences using Comparatorinterface comparemethod.

是的,可以使用Comparator接口compare方法根据不同的排序序列比较两个对象。

You need to create a sort sequence class. Sorting user defined objects using comparator

您需要创建一个排序序列类。使用比较器对用户定义的对象进行排序

回答by Bohemian

Yes, but first you should type the Comparable interface you're implementing. Here's what it should look like:

是的,但首先你应该输入你正在实现的 Comparable 接口。它应该是这样的:

public class Student implements Comparable<Student> {
    private int age;
    private String name;
    @Override
    public int compareTo(Student s) {
        if (name.equals(s.name))
            return age - s.age;
        return name.compareTo(s.name));
    }
}

Notice how with the typed interface Comparable<Student>, instead of the raw type Comparable, there's no need to cast.

注意如何使用类型化接口Comparable<Student>而不是原始类型Comparable,不需要强制转换。

回答by varzeak

Note that you are overloadingthe compareTomethod in your above code, not overriding it.

请注意,您在上面的代码中重载了该compareTo方法,而不是覆盖它。

The interfaceyou are implementing:

interface要实现:

public interface Comparable<T> {
    public int compareTo(T o);
}

Your implementation:

您的实施:

@Override
public int compareTo(Object t) {
    //...
}

The original author of this interface, Josh Bloch, advised in his book Effective Javato use the @Overrideannotation just for this reason; bugs caused by overloading can be rather hard to spot.

该接口的原作者 Josh Bloch 在他的《Effective Java》一书中建议使用@Override注释正是出于这个原因;由过载引起的错误可能很难发现。

You say you want to compare these objects "based on multiple fields"- I'm not sure if this means "order them one way based on two fields" or "order them multiple ways, each based on a single field". Either are possible, as demonstrated in the other answers here.

您说您想“基于多个字段”比较这些对象 - 我不确定这是否意味着“基于两个字段以一种方式对它们进行排序”或“以多种方式对其进行排序,每种方式都基于单个字段”。如此处的其他答案所示,两者都是可能的。

However, this is the bottom line:

然而,这是底线:

You should implement Comparable<T>if you are creating a class yourself, as you appear to be, and want to define a "natural ordering" for this class. If you want to define multiple orderings or you do not control the class in question, define your own ordering in a class that implements Comparator<T>(See here).

Comparable<T>如果您自己创建一个类,就像您看起来的那样,并且想要为这个类定义“自然顺序”,那么您应该实现。如果要定义多个排序或不控制相关类,Comparator<T>实现的类中定义自己的排序(请参阅此处)。

回答by kervin

Apache CompareToBuilderclass is worth a mention.

Apache CompareToBuilder类值得一提。

You can implement with or without reflection

您可以使用或不使用反射来实现

public int compareTo(Object o) {
    return CompareToBuilder.reflectionCompare(this, o);
}