When should a class be Comparable and/or Comparator?

前端 未结 11 2618
醉话见心
醉话见心 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:27

    Comparable is for objects with a natural ordering. The object itself knows how it is to be ordered.
    Comparator is for objects without a natural ordering or when you wish to use a different ordering.

提交回复
热议问题