在比较器接口中使用equals()方法? [英] use of equals() method in comparator interface?

查看:94
本文介绍了在比较器接口中使用equals()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

equals()方法可用于 Object 类中的所有java集合类。此方法也在 Comparator 接口中声明,那么在Comparator中声明此方法的目的是什么?在哪种情况下使用以及如何使用?

equals() method is available to all java collection classes from the Object class. This method is also declared in Comparator interface, so what is the purpose of declaring this method in Comparator? in which case is it used and how?

推荐答案


声明此方法的目的是什么比较器?

what is the purpose of declaring this method in Comparator?

我认为这是设计师强调 Comparator.equals()强加一些其他要求在任何实现该接口的类上:

I think it's the designer's way of highlighting the fact that Comparator.equals() imposes some additional requirements on any classes that implement the interface:


此外,此方法可以返回 true 仅当指定的对象也是比较器并且它与此比较器施加相同的顺序时。因此, comp1.equals(comp2)意味着 sgn(comp1.compare(o1,o2))== sgn(comp2.compare(o1, o2))为每个对象引用 o1 o2

Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.

该方法可用于确定两个不同的比较器是否强加相同的订单。

The method can be used to establish whether or not two distinct comparators impose the same order.

这篇关于在比较器接口中使用equals()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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