In Java, we can sort all the wrapper class-type collections with collections.sort(); method but when it comes to user-defined type collections we have two main approaches: using the Comparable interface or the Comparator interface. While they both serve the same purpose, which is to provide a way to sort objects, they differ in how they achieve it and when to use them. Let's see the major differences between Comparable and Comparator in Java Collections.
Comparable User Interface
The java. lang package includes the Comparable interface, which has just one method: compareTo(). A class can implement a natural ordering of its objects thanks to this interface. A class that complies with the Comparable interface specification can be automatically sorted using the criteria specified in the compareTo() method.
This is how Comparable is used in a class:
import java.util.*;
public class Demo implements Comparable<Demo> {
private int x;
// Constructor, getters, and setters
@Override
public int compareTo(Demo o1) {
return Integer.compare(this.x, o1.x);
}
}
By implementing the Comparable interface, the Demo class in this example indicates that it may be compared to other Demo instances. The 'x' fields of Demo class objects are compared using the compareTo() function.
You can easily sort Demo class objects by using Collections when you have a collection of them, such as a list.sort():
List<Demo> myList = new ArrayList<>();
// Add elements to myList
Collections.sort(myList);
Comparator Interface
The Comparator interface is also found in the java. util package provides a way to define custom sorting orders for objects that may not have a natural ordering. Unlike Comparable, which is implemented within the class of the objects being sorted, Comparator is implemented externally to the class.
Here's how you implement Comparator for the Demo class:
import java. util.*;
public class MyComparator implements Comparator<Demo> {
@Override
public int compare(Demo o1, Demo o2) {
return Integer.compare(o1.getx(), obj2.getx());
}
}
In this example the MyComparator Interface compares defines custom sorting logic in the 'compare()' method
To use this Comparator, you can pass it to sorting methods that accept a Comparator, such as Collections.sort() or Arrays.sort():
List<Demo> myList = new ArrayList<>();
// Add elements to myList
Collections.sort(myList, new MyComparator());
Key Differences
Implementation Location:
The class whose objects need to be sorted implements a comparable interface.
Since the comparator interface is implemented in a different class, the same class can have numerous distinct sorting criteria applied to it.
Method Signature:
Comparable possesses a comparison. using the signature int compareTo(T o) in the To() function.
The signature int compare(T o1, T o2) is used by the comparator's compare() function.
Natural Ordering vs. Custom Ordering:
Comparable gives things a built-in ordering.
When more than one sorting criterion is required or the class itself lacks a natural ordering, the comparator's custom ordering feature comes in handy.
In conclusion, the Comparator is used for external custom ordering, and the Comparable is used for natural ordering within the class. You can select one of them based on your needs and the layout of your classes.
Opmerkingen