Why does Comparator declare equals?

后端 未结 8 1532
无人及你
无人及你 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.

提交回复
热议问题