Why does the Java Collections Framework offer two different ways to sort?

后端 未结 3 1344
忘了有多久
忘了有多久 2020-12-05 22:12

If I have a list of elements I would like to sort, Java offers two ways to go about this.

For example, lets say I have a list of Movie objects and I’d like to sort

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 22:33

    One is for conciseness for what should be a common case (Effective Java 2nd Edition, Item 12: Consider implementing Comparable). The other, as you noted, is for flexibility and general-purposeness.

    Related questions

    • difference between compare() and compareTo()
    • When to use Comparable vs Comparator
    • Comparable and Comparator contract with regards to null
    • Can I use a Comparator without implementing Comparable?

提交回复
热议问题