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.
# 1 楼答案
首先,值得注意的是,该方法并非“抽象化”。如果您实现了
Comparator<T>
,而没有实现equals()
,那么代码将编译。您的类将只使用Object
提供的实现至于为什么要重新声明该方法,这是因为
Comparator<T>.equals()
上的合同比Object.equals()
上的合同更严格。这在documentation中解释:如果
Comparator
没有覆盖equals()
,就没有好的方法来指定它在equals()
上的契约不同于Object