Comparator and Comparable What is the difference?

  Comparable natural ordering interface defines the object, the interface is ordered, there is now assumed that "List list (or array) interface objects implement Comparable class" List the list (or array) by the Collections.sort (or Arrays .sort) order. \


  Comparator interface and is relatively, if not realized Comparable interface, you need to define a class to implement comparators compare Comparator interface method, and then sorted by the class of the comparator
  

  Difference: Comparable always only one, and is in direct comparison, but can have multiple comparator to define the order of objects

Guess you like

Origin www.cnblogs.com/nyhhd/p/12538987.html