Comparator Vs Comparable In Java
As a Java Developer we always come Across these two Interfaces when we need to sort any custom Object ,
lets discuss its use cases:-
COMPARABLE INTERFACE
- Comparable Interface provides us compareTo method to be overriden , It becomes the property of a class and their can exist only one comparable overriding for Class .
- We can only use Comparable Interface , when the class is our internal class and we want to define a definite natural ordering for it.
public class Comparing implements Comparable<Comparing> {
private int x;
private String y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public Comparing(int x, String y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Comparing{" +
"x=" + x +
", y='" + y + '\'' +
'}';
}
@Override
public int compareTo(Comparing o) {
if(this.getX() == o.getX()){
return this.getY().compareTo(o.getY());
}
else {
return this.getX() > o.getX() ? 1 : -1 ;
}
}
}
Here In this example we can see , we have overriden the compareTo method in our custom Class Comparing .
Comparing comparing1 = new Comparing(1,"a");
Comparing comparing2 = new Comparing(2,"c");
Comparing comparing3 = new Comparing(4,"b");
List<Comparing> list = new ArrayList<>();
list.add(comparing1);
list.add(comparing2);
list.add(comparing3);
Collections.sort(list);
Note :- If we dont override the compareTo method in our class , Collections.sort(list) will give compile time error .
COMPARATOR INTERFACE
Suppose the Class Objects we want to sort is an external class , In that case we dont have control over source code of that class , how can we sort the list in such cases .
Suppose we want to have multiple sorting logic for a class , which can be used in different scenarios .
These two cases are not addressed by Comparable Interface , Here Comparator Interface come to rescue .
- Comparator interface provides a compare method , which we can override to create multiple sorting logic for a class.
- It doesn’t required a change in source code in client class which we want to sort.
public class Comparing {
private int x;
private String y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public Comparing(int x, String y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Comparing{" +
"x=" + x +
", y='" + y + '\'' +
'}';
}
}
Comparing comparing1 = new Comparing(1,"a");
Comparing comparing2 = new Comparing(2,"c");
Comparing comparing3 = new Comparing(3,"b");
List<Comparing> list = new ArrayList<>();
list.add(comparing1);
list.add(comparing2);
list.add(comparing3);
Collections.sort(list,Comparator.comparing(Comparing::getX));
Collections.sort(list,Comparator.comparing(Comparing::getY));
Here , We can see we can create any comparator and pass it to the Collections.sort(list, customComparator) and we have a new sorting logic.
Related Articles: -
https://medium.com/@javabackend175/final-keyword-in-java-c197c6a28345
https://medium.com/@javabackend175/multithreaded-singleton-java-class-f3b7d633752b
https://medium.com/@javabackend175/hashcode-method-in-object-class-36f6abefa299
Please follow on medium for more such inquisitive and detailed articles.
https://twitter.com/JavaUnlocked