Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. Comparable vs Comparator
  2.  
  3. Interface __Comparable__ contains method public int __compareTo(E a, E b)__ and is usually implemented in classes for natural
  4. ordering or default ordering.
  5.  
  6. Interface __Comparator__ contains method public int __compare(E a, E b)__ and is tailored for custom comparison and custom sorting
  7. order.
  8.  
  9. So, there are usually two methods for sorting collections, the first takes a collection only and sorts it using its in-built
  10. __compareTo()__ for a given class that is already implemented.
  11.  
  12. The other one takes two arguments, one of which is the collection and the second is the instance of Comparator,
  13. whose compare method is used for custom sort. It is convenient to use __Comparator__ as a functional interface and define it
  14. by a lambda or stack a priority of comparing values by using __Comparator().thenComparing(AnotherComparator())__;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement