使用compareTo实现equals方法 [英] Implementing equals method using compareTo

查看:177
本文介绍了使用compareTo实现equals方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般问题:在Java中实现对默认 equals 方法的覆盖时,我应该关注的是如何简单地使用已经实现的 compareTo 方法vs将独立逻辑写入equals方法?我注意到有人在另一个问题中提到 foo.equals((String)null)返回false而 String.compareTo((String)null)抛出 NullPointerException 。是什么使这些不一致的结果成为理想的功能?

General question: When implementing an override of the default equals method in Java, what concerns should I have about simply utilizing an already implemented compareTo method vs writing independent logic into the equals method? I noticed someone mention in another question that foo.equals((String)null) returns false whereas String.compareTo((String)null) throws a NullPointerException. What makes these inconsistent results ideal functionality?

示例等于方法:

@Override
public boolean equals(Object obj) {
    if (obj != null && obj instanceof MyClass) {
        MyClass msg = (MyClass)obj;
        return this.compareTo(msg) == 0;
    }
    return false;
}

编辑:
来自文档的引用在可比较


当且仅当e1.compareTo(e2)== 0具有相同的布尔值时,C类的自然顺序被认为与
等于一致对于C类的每个e1和e2,将
作为e1.equals(e2)。注意null不是
任何类的实例,而e.compareTo(null)应该抛出
NullPointerException即使e.equals(null)返回false

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false

编辑:

经过进一步审核后,我发现可比较的文档还说明了以下内容:

After further review, I find it of note that the Comparable documentation also states the following:


实现者必须确保sgn(x.compareTo(y) ))== -sgn(y.compareTo(x))表示所有x和y。 (这意味着如果y.compareTo(x)抛出异常,x.compareTo(y)必须抛出异常。)

The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

Ergo ,因为 null.compareTo(x)显然会抛出 NPE x.compareTo(null)也应该抛出NPE。而对于平等,情况不一定如此。我对NPE的正确处理非常重要,所以我发现它相对重要。

Ergo, since null.compareTo(x) obviously throws a NPE, x.compareTo(null) should throw a NPE as well. Whereas for equals, that is not necessarily the case. I am pretty big on the proper handling of NPEs, so I find this relatively significant.

推荐答案

<$ c $之间的区别c> equals()和 compareTo()等于()只检查是否两个对象彼此相等,其中 compareTo()用于标识指定类的实例的自然顺序。另外 equals()方法与 hashCode()方法有合约,但 compareTo()还没有。

The difference between equals() and compareTo() is that equals() just checks if two objects are equal to each other where the compareTo() is used to identify the natural order of the instances of specified class. Also equals() method has a contract with hashCode() method but compareTo() hasn't.

根据 JavaDoc


请注意,null不是实例任何类,并且e.compareTo(null)
应该抛出NullPointerException,即使e.equals(null)返回
false。

Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

它强烈推荐,但并非严格要求
(x.compareTo(y)== 0)==(x.equals(y))。一般来说,实现Comparable接口并违反此条件
的任何类
都应清楚地表明这一事实。推荐的语言是注意:
这个类的自然顺序与equals不一致。

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

你可以感受到可以在 equals()方法中重用 compareTo()方法逻辑但请记住<的所有合同code> equals(), hashCode()和来自JavaDoc的合约 compareTo()方法。如果他们不相互冲突,那就继续吧。

You can feel free to reuse compareTo() method logic in your equals() method but keep in mind all the contracts to the equals(), hashCode() and contract from JavaDoc for compareTo() method. If they do not conflict with each other then go ahead.

我认为执行合同更为重要。

I think that the enforcement of contracts is more important point.

这篇关于使用compareTo实现equals方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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