Pages

December 25, 2017

#Java8 : Comparator example to sort a List using Lambda expression..

1). Create a Employee class:
public class Employee {   
    private String name;
    private int age;
   

    public Employee(String name, int age)
    {
        this.name=name;
        this.age=age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
   
    @Override
    public String toString() {
        return "name:"+this.name+", age:"+this.age;
    }   
}

2). The sorting is done in EmployeeSorting
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class EmployeeSorting {
    /**
     * This method will create list of employees
     * @return employees
     */
    private static List < Employee >  getEmployeeList() {
        List < Employee >  employees = new ArrayList < Employee > ();
        employees.add(new Employee("Andreas Ehmich", 33));
        employees.add(new Employee("Michael Andrew", 54));
        employees.add(new Employee("Chamuel Matthew", 45));
        employees.add(new Employee("Tiger Khan", 27));
        employees.add(new Employee("Himaanshu Shuklaa", 32));
        return employees;
    }
   
    /**
     * This class will sort the list of employees using Collections.sort.
     * We are passing anonymous Comparator class for sorting.
     */
    public static void sortWithoutLamda() {   
        List < Employee >  employees = getEmployeeList();
        System.out.println("Printing employees before sorting..");
        for (Employee employee : employees) {
            System.out.println(employee);
        }
        /* Sorting without Lamda */
        System.out.println("Sorting employees based on the names..");
        Collections.sort(employees, new Comparator < Employee > () {
            @Override
            public int compare(Employee employee1, Employee employee2) {
                return employee1.getName().compareTo(employee2.getName());
            }
        });
        System.out.println("Printing employees after sorting on the basis of name..");
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }
   
    /**
     * In this method we will directly use Java 8's sort method, instead of Collections.sort.
     * In the sort method, instead of passing anonymous Comparator class, Lamda expression is used.
     * For printing the employees, forEach method of Java 8 is used.
     */
    public static void sortWithLamda() {
        List < Employee >  employees = getEmployeeList();
        System.out.println("Printing employees before sorting..");
        for (Employee employee : employees) {
            System.out.println(employee);
        }
        /* Sorting with Lamda */
        System.out.println("Sorting employees based on the names using Lamda expression..");
        employees.sort((employee1, employee2)- > employee1.getName().compareTo(employee2.getName()));
       
        System.out.println("Printing employees using Lamda expression after sorting on the basis of name..");
        employees.forEach((employee)- > System.out.println(employee));
               
        System.out.println("Sorting employees based on the age using Lamda expression..");
        employees.sort((employee1, employee2)- > employee1.getAge() -employee2.getAge());
       
        System.out.println("Printing employees using Lamda expression after sorting on the basis of age..");
        employees.forEach((employee)- > System.out.println(employee));
    }
    public static void main(String[] args)
    {       
        System.out.println("##Sort without Lamda called..");
        sortWithoutLamda();
        System.out.println("##Sort with Lamda called..");
        sortWithLamda();
    }
}

Also Check : Java 8 Blogs

No comments:

Post a Comment