When should a class be Comparable and/or Comparator?

前端 未结 11 2638
醉话见心
醉话见心 2020-11-22 14:11

I have seen classes which implement both Comparable and Comparator. What does this mean? Why would I use one over the other?

11条回答
  •  暖寄归人
    2020-11-22 14:29

    Comparable is for providing a default ordering on data objects, for example if the data objects have a natural order.

    A Comparator represents the ordering itself for a specific use.

提交回复
热议问题