What is the purpose of Comparator Interface? 1. Comparators can be used to control the order of certain data structures and collection of objects too. 2. The interface can be found in java.util.Comparator 3. A Comparator must define a compare function which takes two Objects and returns a -1, 0, or 1 4. Sorting can be done implicitly by using data structures or by implementing sort methods explicitly. What is the concept Comparator Interface? Natural ordering is established by using Comparator interface. This interface can be used to sort the objects which does not have natural sorting order.
Comparator interface is passed to sort methods or to have precise control over the sort order. The order of certain data structures such as sorted sets, sorted maps which do not have a natural ordering.
|