Java.Util.Comparator is an interface used for sorting
collections. The classes implementing Comparators must implement the compare()
method. Here is a simple example of how a comparator can be used. In this
example we have a simple pojo named Employee.java, 3 simple Comparator classes
and a main class to demonstrate the functionality.
Employee.java:
package
com.soft.model;
public
class Employee {
private
String name;
private
String department;
private
int age;
public
String getName() {
return name;
}
public
void setName(String name) {
this.name = name;
}
public
String getDepartment() {
return department;
}
public
void setDepartment(String department) {
this.department = department;
}
public
int getAge() {
return age;
}
public
void setAge(int age) {
this.age = age;
}
}
EmployeeNameComparator.java:
package
com.soft.model;
import
java.util.Comparator;
public
class EmployeeNameComparator implements Comparator
public int
compare(Object obj1, Object obj2) {
Employee e1=(Employee)obj1;
Employee e2=(Employee)obj2;
return e1.getName().compareTo(e2.getName());
}
}
EmployeeDepartmentComparator.java
package
com.soft.model;
import
java.util.Comparator;
public
class EmployeeDepartmentComparator implements
Comparator
public int
compare(Object obj1, Object obj2) {
Employee e1=(Employee)obj1;
Employee e2=(Employee)obj2;
return
e1.getDepartment().compareTo(e2.getDepartment());
}
}
EmployeeAgeComparator.java:
package
com.soft.model;
import
java.util.Comparator;
public
class EmployeeAgeComparator implements Comparator
public int
compare(Object obj1, Object obj2) {
Employee e1=(Employee)obj1;
Employee e2=(Employee)obj2;
if(e1.getAge()>e2.getAge())
{
return
1;
}else if(e1.getAge()
return
-1;
} else{
return
0;
}
}
}
ComparatorExample.java:
package com.soft.examples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.soft.model.Employee;
import com.soft.model.EmployeeAgeComparator;
import com.soft.model.EmployeeDepartmentComparator;
import com.soft.model.EmployeeMultipleFieldsComparator;
import com.soft.model.EmployeeNameComparator;
public class ComparatorExample {
public static void main(String[] args) {
List<Employee>
list= new ArrayList<Employee>();
Employee e=new Employee();
e.setName("Danial Fritz");
e.setDepartment("Field Based Development");
e.setAge(36);
list.add(e);
e=new Employee();
e.setName("Denis Khoo");
e.setDepartment("Project Management");
e.setAge(30);
list.add(e);
e=new Employee();
e.setName("Joe Kum");
e.setDepartment("Adminstration");
e.setAge(40);
list.add(e);
e=new Employee();
e.setName("Anil Kumar");
e.setDepartment("Information Technology");
e.setAge(26);
list.add(e);
System.out.println("************ Before Sorting
***********");
for (int i=0;i< list.size();i++){
System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
}
System.out.println("************ Name Sorting ***********");
Collections.sort(list, new EmployeeNameComparator());
for (int i=0;i< list.size();i++){
System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
}
System.out.println("************ Department Sorting
***********");
Collections.sort(list, new EmployeeDepartmentComparator());
for (int i=0;i< list.size();i++){
System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
}
System.out.println("************ Age Sorting
***********");
Collections.sort(list, new EmployeeAgeComparator());
for (int i=0;i< list.size();i++){
System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
}
}
}
This sorting can also be done through java.lang.Comparable, the implementation
of which is given at
How to implement a java.lang.Comparable
The differences between java.util.Comparator and java.lang.Comparable are given at
How to implement a java.lang.Comparable
The differences between java.util.Comparator and java.lang.Comparable are given at
No comments:
Post a Comment