Java sorting Comparable and Comparator
1. Comparable in java is part of java.lang package, where as
Comparator is part of java.util package
2. Comparable has compareTo(Object) method for sorting, where as
Comparator has compare(Object, Object) method for sorting.
3. Using comparable we can sort only on a single entity of a class either age, sal or name at a time.
Using comparator we can create multiple sorting classes on a single entity of a class, which we can pass as an object to the compare(object, object) method.
Note : We need to implement the Comparable or Comparator interface only when we need to sort the data on custom classes.
Eg :
public class Employee implements Comparable{
int age;
String name;
Employee (int age, String name){
this.age=age;
this.name=name;
}
....
public int compareTo(Object o){
Employee e=(Employee)o;
return e.getAge()- this.getAge();
}
}
Here Employee is a custom class and we have implemented the Comparable interface to sort the employees based on age.
In order for the sorting to happen, we need call Collections.sort(list) method. Here is the snippet
public static void main(String args[]){
List<Employee> l1=new ArrayList<Employee>();
l1.add(new Employee(25,"Sharad"));
l1.add(new Employee(24,"Naveen"));
l1.add(new Employee(22,"Gopi"));
l1.add(new Employee(21,"Ramesh"));
l1.add(new Employee(35,"Sunil"));
Collections.sort(l1);
for(Employee e : l1){
System.out.println(" Age : "+e.getAge()+" : Name : "+e.getName());
}
}
In case we call the Collections.sort(list); method and we do not implement the Comparable interface, we will get ClassCastException as the Employee object is not compatible with Object parameter in compareTo(Object o) method.
---Will Update with Comparator data soon.----
Please provide your valuable comments on this post to improve this blog.
1. Comparable in java is part of java.lang package, where as
Comparator is part of java.util package
2. Comparable has compareTo(Object) method for sorting, where as
Comparator has compare(Object, Object) method for sorting.
3. Using comparable we can sort only on a single entity of a class either age, sal or name at a time.
Using comparator we can create multiple sorting classes on a single entity of a class, which we can pass as an object to the compare(object, object) method.
Note : We need to implement the Comparable or Comparator interface only when we need to sort the data on custom classes.
Eg :
public class Employee implements Comparable{
int age;
String name;
Employee (int age, String name){
this.age=age;
this.name=name;
}
....
public int compareTo(Object o){
Employee e=(Employee)o;
return e.getAge()- this.getAge();
}
}
Here Employee is a custom class and we have implemented the Comparable interface to sort the employees based on age.
In order for the sorting to happen, we need call Collections.sort(list) method. Here is the snippet
public static void main(String args[]){
List<Employee> l1=new ArrayList<Employee>();
l1.add(new Employee(25,"Sharad"));
l1.add(new Employee(24,"Naveen"));
l1.add(new Employee(22,"Gopi"));
l1.add(new Employee(21,"Ramesh"));
l1.add(new Employee(35,"Sunil"));
Collections.sort(l1);
for(Employee e : l1){
System.out.println(" Age : "+e.getAge()+" : Name : "+e.getName());
}
}
In case we call the Collections.sort(list); method and we do not implement the Comparable interface, we will get ClassCastException as the Employee object is not compatible with Object parameter in compareTo(Object o) method.
---Will Update with Comparator data soon.----
Please provide your valuable comments on this post to improve this blog.
Comments
Post a Comment