比较器接口的equals方法,为什么总是安全地不重写Object.equals(Object) [英] Comparator interface's equals method, why it is always safe not to override Object.equals(Object)

查看:110
本文介绍了比较器接口的equals方法,为什么总是安全地不重写Object.equals(Object)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究 Comparator 接口,并注意到在



我已经检查了< equals 中默认方法的实现。 code> Object 类





因此使用 equals 方法的默认实现,它只是检查两个实例是否es指向同一对象,因为 this == obj 测试引用相等性。



但是,如果我有两个 Comparator 实例,它们返回的结果是相同的,我想知道它们是否等效。如果我不重写默认的 Object的equals 方法,则不管它们返回的结果是否相等,都可以使用默认的 Object's equals 方法,始终返回 false 。因此,不重写Object.equals(Object)是否仍然总是安全的?

解决方案

I假设您误解了Java文档的意思:


此方法可以返回true only 如果指定的对象也是一个比较器,并且施加与该比较器相同的顺序


默认实现将返回true (如果它是完全相同的对象),根据定义,这意味着它们(实际上是单个)都提供相同的排序顺序



此定义不暗示即使不同的比较器提供相同的排序顺序,它也将返回true,简单的验证:

  import java.util。比较器

公共类TestComparator {
静态类Comparator1实现Comparator< Integer> {
@Override
public int compare(final Integer o1,final Integer o2){
return Integer.compare(o1,o2);
}
}

静态类Comparator2实现Comparator< Integer>。 {
@Override
public int compare(final Integer o1,final Integer o2){
return Integer.compare(o1,o2);
}
}

public static void main(final String [] args){
final Comparator1 c1 = new Comparator1();
final Comparator1 c11 =新的Comparator1();
final Comparator2 c2 =新的Comparator2();

System.out.println(c1.equals(c1)); // true
System.out.println(c1.equals(c11)); // false
System.out.println(c1.equals(c2)); // false
}
}

默认实现可以如果比较器等效,则返回true,也可以返回false(如果对象不同)



请注意,文档说:


但是,在某些情况下,重写此方法可能会通过允许程序确定两个与众不同的来提高性能。比较器会施加相同的顺序


因此,可以安全地不覆盖它,但是不足以保证不同但等效的比较器将被正确地比较


I'm currently studying the Comparator interface and noticed that in the documentation for Comparator's equals method, it states

Note that it is always safe not to override Object.equals(Object)

I have checked the implementation for the default equals method in Object class

So with the default implementation of equals method, it simply checks whether two instances points to the same object because this == obj tests for reference equality.

But what happen if I have two instances of Comparator, the result they return are identical, and I want to know if they are equivalent. If I dont override the default Object's equals method, then regardless of whether the result they return is equivalent or not, by using the default Object's equals method, false will always be returned. So is it still always safe not to override the Object.equals(Object)?

解决方案

I suppose you mis-interpreted what java doc is saying:

this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator

Default implementation will return true only if it is exactly the same object, which by definition means that they both (actually single one) provide the same sorting order

This definition does not imply that it will return true for different comparators even if they provide the same sorting order, simple verification:

import java.util.Comparator;

public class TestComparator {
    static class Comparator1 implements Comparator<Integer> {
        @Override
        public int compare(final Integer o1, final Integer o2) {
            return Integer.compare(o1, o2);
        }
    }

    static class Comparator2 implements Comparator<Integer> {
        @Override
        public int compare(final Integer o1, final Integer o2) {
            return Integer.compare(o1, o2);
        }
    }

    public static void main(final String[] args) {
        final Comparator1 c1 = new Comparator1();
        final Comparator1 c11 = new Comparator1();
        final Comparator2 c2 = new Comparator2();

        System.out.println(c1.equals(c1)); // true
        System.out.println(c1.equals(c11)); // false
        System.out.println(c1.equals(c2)); // false
    }
}

Default implementation can return true if comparators equivalent, also it can return false (if objects are different)

Note further, doc says:

However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct comparators impose the same order.

So, it is safe not to override, but it is not enough to guarantee that different but equivalent comparators will be compared properly

这篇关于比较器接口的equals方法,为什么总是安全地不重写Object.equals(Object)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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