Why does Comparator declare equals?

后端 未结 8 1530
无人及你
无人及你 2021-02-05 04:34

The Comparator interface has its own equals() method. Any class will get equals() by default through Object class. What is the need to have equal

相关标签:
8条回答
  • 2021-02-05 04:52

    Technically, the declaration of the method is redundant (the compiler does not care), but...

    Declaring the equals method in this interface makes it part of the contract between caller and different Comparators and allows it to specify/extend its semantics.

    It specifies that two Comparators are equal only if they impose the same ordering with their compare() method. This extends the semantics of Object.equals() and must therefore be documented in the interface.

    0 讨论(0)
  • 2021-02-05 04:53

    Putting an Object method in an interface declaration allows Javadoc declaration of the meaning equals is required to have in classes that implement the interface.

    0 讨论(0)
提交回复
热议问题