Sorting Employee using Java 8 Comparator


This  is   simple  example  of java8  comparator.  Which simplify  the  sorting  of  any collection easily. In this example  we try to sort  the Employee class in many way .





import java.util.ArrayList;

import java.util.Comparator;

import java.util.List;


public class JavaComprator {


private static List<Employee> getEmployees(){

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

    employees.add(new Employee(9,"Sunil ", "Chopra", 25));

    employees.add(new Employee(4,"Akash", "Sharma", 28));

    employees.add(new Employee(7," Mukesh", " Ambani", 52));

    employees.add(new Employee(1," Yamini", " dutta", 19));

    employees.add(new Employee(11," Babulu", "pandey", 72));

    employees.add(new Employee(5," Tinku", " Thakur", 88));

    employees.add(new Employee(5," Anjani", " Lal", 36));

    employees.add(new Employee(46," Gurucharan", "kumhar", 59));

    employees.add(new Employee(15," Deepak", "jha", 32));

    employees.add(new Employee(7, "Bhavya", "Jha", 33));

    return employees;

}

public  static void  main( String   args[]) {

List <Employee>  employee =getEmployees();

 

  System.out.println("Sorting Employee according to Name") ;

 

 

    employee.sort( Comparator.comparing(Employee::getFirstName));

   

          System.out.println(employee);


  System.out.println("Sorting Employee according to Age") ;


    employee.sort( Comparator.comparing(Employee::getAge));


    System.out.println(employee);

   

   

  System.out.println("Sorting Employee First then  Age ") ;



    Comparator<Employee> groupByComparator = Comparator.comparing(Employee::getFirstName)

                    .thenComparing(Employee::getAge);

   

    employee.sort(groupByComparator);

   

    System.out.println(employee);

}

}



Employee  class 



public class Employee {


private Integer id;

private String firstName;

    private String lastName;

    private Integer age;

     

    public  Employee(Integer id, String firstName, String lastName, Integer age){

        this.id = id;

        this.firstName = firstName;

        this.lastName = lastName;

        this.age = age;

    }

    

    public Integer getId() {

return id;

}


public void setId(Integer id) {

this.id = id;

}


public String getFirstName() {

return firstName;

}


public void setFirstName(String firstName) {

this.firstName = firstName;

}


public String getLastName() {

return lastName;

}


public void setLastName(String lastName) {

this.lastName = lastName;

}


public Integer getAge() {

return age;

}


public void setAge(Integer age) {

this.age = age;

}


    

    

    

 public String toString() {

        return "\n["+this.id+","+this.firstName+","+this.lastName+","+this.age+"]"; 

    }

}





After   the  successful  execution   output would like this .


Sorting Employee according to Name

[

[5, Anjani, Lal,36], 

[11, Babulu,pandey,72], 

[15, Deepak,jha,32], 

[46, Gurucharan,kumhar,59], 

[7, Mukesh, Ambani,52], 

[5, Tinku, Thakur,88], 

[1, Yamini, dutta,19], 

[4,Akash,Sharma,28], 

[7,Bhavya,Jha,33], 

[9,Sunil ,Chopra,25]]

Sorting Employee according to Age

[

[1, Yamini, dutta,19], 

[9,Sunil ,Chopra,25], 

[4,Akash,Sharma,28], 

[15, Deepak,jha,32], 

[7,Bhavya,Jha,33], 

[5, Anjani, Lal,36], 

[7, Mukesh, Ambani,52], 

[46, Gurucharan,kumhar,59], 

[11, Babulu,pandey,72], 

[5, Tinku, Thakur,88]]

Sorting Employee First then  Age 

[

[5, Anjani, Lal,36], 

[11, Babulu,pandey,72], 

[15, Deepak,jha,32], 

[46, Gurucharan,kumhar,59], 

[7, Mukesh, Ambani,52], 

[5, Tinku, Thakur,88], 

[1, Yamini, dutta,19], 

[4,Akash,Sharma,28], 

[7,Bhavya,Jha,33], 

[9,Sunil ,Chopra,25]]





Sorting Employee using Java 8 Comparator Sorting  Employee using Java 8 Comparator Reviewed by Mukesh Jha on 5:29 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.