Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Comparable vs Comparator
- Interface __Comparable__ contains method public int __compareTo(E a, E b)__ and is usually implemented in classes for natural
- ordering or default ordering.
- Interface __Comparator__ contains method public int __compare(E a, E b)__ and is tailored for custom comparison and custom sorting
- order.
- So, there are usually two methods for sorting collections, the first takes a collection only and sorts it using its in-built
- __compareTo()__ for a given class that is already implemented.
- The other one takes two arguments, one of which is the collection and the second is the instance of Comparator,
- whose compare method is used for custom sort. It is convenient to use __Comparator__ as a functional interface and define it
- 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