Thursday, 9 July 2015

Java : Collection Framework : Collections (ReverseOrder Comparator)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=vU1uiwOavLM&list=UUhwKlOVR041tngjerWxVccw

Employee.java
public class Employee
{
    private Integer    employeeId;
    private String name;

    public Employee( Integer employeeId, String name )
    {
        super();
        this.employeeId = employeeId;
        this.name = name;
    }

    public Integer getEmployeeId()
    {
        return employeeId;
    }

    public void setEmployeeId( int employeeId )
    {
        this.employeeId = employeeId;
    }

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return "Employee [employeeId=" + employeeId + ", name=" + name + "]";
    }

}
AscEmployeeIdComparator.java
import java.util.Comparator;

public class AscEmployeeIdComparator implements Comparator<Employee>
{

    /*
     * This method used to order the employee objects in Ascending order based
     * on employeeId.
     */
    @Override
    public int compare(Employee employee1, Employee employee2)
    {

        return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());

    }

}
CollectionsExample.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
 Method 

 public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)

 Parameters:

 cmp - a comparator who's ordering is to be reversed by the returned comparator or null

 Returns:

 A comparator that imposes the reverse ordering of the specified comparator.

 */

public class CollectionsExample
{

    public static void main(String[] args)
    {

        List<Employee> employeeList = new ArrayList<Employee>();

        Employee john = new Employee(300, "John");
        Employee david = new Employee(100, "David");
        Employee peter = new Employee(200, "Peter");
        Employee juli = new Employee(500, "Juli");
        Employee ram = new Employee(400, "Ram");

        employeeList.add(john);
        employeeList.add(david);
        employeeList.add(peter);
        employeeList.add(juli);
        employeeList.add(ram);

        System.out.println("Before sort the employeeList : \n");
        displayEmployeeInfo(employeeList);

        AscEmployeeIdComparator ascEmployeeIdComparator = new AscEmployeeIdComparator();

        Collections.sort(employeeList, ascEmployeeIdComparator);

        System.out
                .println("\nAfter Sort the employeeList using ascEmployeeIdComparator : \n");
        displayEmployeeInfo(employeeList);

        /*
         * Returns a comparator that imposes the reverse ordering of the
         * specified comparator. If the specified comparator is null, this
         * method is equivalent to reverseOrder() (in other words, it returns a
         * comparator that imposes the reverse of the natural ordering on a
         * collection of objects that implement the Comparable interface).
         */

        Comparator<Employee> reverseEmployeeIdcomparator = Collections
                .reverseOrder(ascEmployeeIdComparator);

        // sort the list
        Collections.sort(employeeList, reverseEmployeeIdcomparator);

        System.out
                .println("\nAfter Sort the employeeList using reverseEmployeeIdcomparator : \n");
        displayEmployeeInfo(employeeList);

    }

    private static void displayEmployeeInfo(List<Employee> employeeList)
    {
        for (Employee employee : employeeList)
        {
            System.out.println("ID :" + employee.getEmployeeId() + " Name :"
                    + employee.getName());

        }
    }
}
Output
Before sort the employeeList : 

ID :300 Name :John
ID :100 Name :David
ID :200 Name :Peter
ID :500 Name :Juli
ID :400 Name :Ram

After Sort the employeeList using ascEmployeeIdComparator : 

ID :100 Name :David
ID :200 Name :Peter
ID :300 Name :John
ID :400 Name :Ram
ID :500 Name :Juli

After Sort the employeeList using reverseEmployeeIdcomparator : 

ID :500 Name :Juli
ID :400 Name :Ram
ID :300 Name :John
ID :200 Name :Peter
ID :100 Name :David
To Download CollectionsDemo-ReverseOrderComparator Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-ReverseOrderComparator.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge