Both Comparator and Comparable interfaces provide features to sort the collections of Objects. However java provided some default methods to sort primitive types arrays and array of objects etc using .sort() method etc, we will use Comparator and comparable interfaces to sort collections.
We will get all details how to use Comparable and Comparator interfaces in my previous posts. Here we can find some basic differences between Comparable and Comparator interface.
1. Sorting : We can sort in single way with Comparable Interface. like we can apply sorting with one of the property of bean to sort list of beans.
@Override
public int compareTo(EmployeeComparableClass o) {
return this.getId() - o.getId(); // compare based on id
}
We can sort collection of objects in different ways using Comparator interface. We can write our custom comparator classes and can use different properties to sort the collections.
//compare and sort based on emp is
@Override
public int compare(EmployeeClass o1, EmployeeClass o2) {
return o1.getId() - o2.getId();
}
or
//compare and sort based on emp name
@Override
public int compare(EmployeeClass o1, EmployeeClass o2) {
return o1.getName().compareTo(o2.getName());
}
2. Implements: Class needs to implement Comparable class in order to sort.
Class no need to do any implement if we go for Comparator. We can crate new comparator classes by implementing the comparator interfaces.
To sort employee, we need to implement Comparable interface as shown below.
public class EmployeeComparableClass implements Comparable<EmployeeComparableClass>
It is not required if we go for Comparator interface.
3. Package: Comparable interface is available under java.lang package where as Comparator interface is available in java.util package
4.Code changes: We no need to do any code changes from client side if we go for Arrays.sort(), Collections.sort() and Comparable interfaces. Here these will use compareTo() method automatically.
We need to do code changes at client side if we go for Comparator interface. Here Client needs to provide comparator class to use compare() method.
Ex: Detailed example is available @Comparable and Comparator
Sorting method
int[] numbers = new int[] { 1, 6, 3, 8, 9 };
System.out.println("without sort " + Arrays.toString(numbers));
Arrays.sort(numbers);
System.out.println("sort array " + Arrays.toString(numbers));
Comparator:
List<EmployeeClass> employess = new ArrayList<>();
employess.add(employeeClass3);
employess.add(employeeClass2);
employess.add(employeeClass1);
employess.add(employeeClass);
System.out.println("before comarator sorting....");
for (EmployeeClass employeeClass4 : employess) {
System.out.println(employeeClass4.toString());// no order it will print emp details randomly
}
System.out.println("after comarator sorting using emp name....");
Collections.sort(employess, new ComparatorNameTest());
Comparable:
EmployeeComparableClass[] classes = new EmployeeComparableClass[4];
EmployeeComparableClass employeeClass = new EmployeeComparableClass(60, "Java", 345678);
EmployeeComparableClass employeeClass1 = new EmployeeComparableClass(50, "J2ee", 9876543);
EmployeeComparableClass employeeClass2 = new EmployeeComparableClass(30, "Spring", 12345);
EmployeeComparableClass employeeClass3 = new EmployeeComparableClass(40, "Jdbc", 654);
classes[0] = employeeClass;
classes[1] = employeeClass3;
classes[2] = employeeClass2;
classes[3] = employeeClass1;
// below it will throw an runtime exception
//Exception in thread "main" java.lang.ClassCastException: com.sample.java.testing.EmployeeClass cannot be cast to java.lang.Comparable
//hence EmployeeComparableClass implement Comparable interface
//SO by using comparable we can sort the list based on
System.out.println("sorting list as per comparable...");
Arrays.sort(classes);
for (EmployeeComparableClass course : classes) {
System.out.println(course.toString());// sorting based on id
}
We will get all details how to use Comparable and Comparator interfaces in my previous posts. Here we can find some basic differences between Comparable and Comparator interface.
1. Sorting : We can sort in single way with Comparable Interface. like we can apply sorting with one of the property of bean to sort list of beans.
@Override
public int compareTo(EmployeeComparableClass o) {
return this.getId() - o.getId(); // compare based on id
}
We can sort collection of objects in different ways using Comparator interface. We can write our custom comparator classes and can use different properties to sort the collections.
//compare and sort based on emp is
@Override
public int compare(EmployeeClass o1, EmployeeClass o2) {
return o1.getId() - o2.getId();
}
or
//compare and sort based on emp name
@Override
public int compare(EmployeeClass o1, EmployeeClass o2) {
return o1.getName().compareTo(o2.getName());
}
2. Implements: Class needs to implement Comparable class in order to sort.
Class no need to do any implement if we go for Comparator. We can crate new comparator classes by implementing the comparator interfaces.
To sort employee, we need to implement Comparable interface as shown below.
public class EmployeeComparableClass implements Comparable<EmployeeComparableClass>
It is not required if we go for Comparator interface.
3. Package: Comparable interface is available under java.lang package where as Comparator interface is available in java.util package
4.Code changes: We no need to do any code changes from client side if we go for Arrays.sort(), Collections.sort() and Comparable interfaces. Here these will use compareTo() method automatically.
We need to do code changes at client side if we go for Comparator interface. Here Client needs to provide comparator class to use compare() method.
Ex: Detailed example is available @Comparable and Comparator
Sorting method
int[] numbers = new int[] { 1, 6, 3, 8, 9 };
System.out.println("without sort " + Arrays.toString(numbers));
Arrays.sort(numbers);
System.out.println("sort array " + Arrays.toString(numbers));
Comparator:
List<EmployeeClass> employess = new ArrayList<>();
employess.add(employeeClass3);
employess.add(employeeClass2);
employess.add(employeeClass1);
employess.add(employeeClass);
System.out.println("before comarator sorting....");
for (EmployeeClass employeeClass4 : employess) {
System.out.println(employeeClass4.toString());// no order it will print emp details randomly
}
System.out.println("after comarator sorting using emp name....");
Collections.sort(employess, new ComparatorNameTest());
Comparable:
EmployeeComparableClass[] classes = new EmployeeComparableClass[4];
EmployeeComparableClass employeeClass = new EmployeeComparableClass(60, "Java", 345678);
EmployeeComparableClass employeeClass1 = new EmployeeComparableClass(50, "J2ee", 9876543);
EmployeeComparableClass employeeClass2 = new EmployeeComparableClass(30, "Spring", 12345);
EmployeeComparableClass employeeClass3 = new EmployeeComparableClass(40, "Jdbc", 654);
classes[0] = employeeClass;
classes[1] = employeeClass3;
classes[2] = employeeClass2;
classes[3] = employeeClass1;
// below it will throw an runtime exception
//Exception in thread "main" java.lang.ClassCastException: com.sample.java.testing.EmployeeClass cannot be cast to java.lang.Comparable
//hence EmployeeComparableClass implement Comparable interface
//SO by using comparable we can sort the list based on
System.out.println("sorting list as per comparable...");
Arrays.sort(classes);
for (EmployeeComparableClass course : classes) {
System.out.println(course.toString());// sorting based on id
}
No comments:
Post a Comment