Why does Comparator declare equals?

后端 未结 8 1536
无人及你
无人及你 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:37

    Read its javadoc. It's there only to explain what equals() must return if you choose to override it in a class implementing Comparator. You might think that no comparator can be equal to any other, but it's not the case. You might think that two comparators are equal if they return the same thing for any arguments, but it's not the case. The javadoc explains that two comparators are equal if they impose the same ordering, whatever the arguments given. The javadoc also says:

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

    Most of the time, you don't override equals() in comparators.

提交回复
热议问题