Java 8 Lamba Expression examples using Comparator

Java 8 has made comparator more powerful using Lambda Expression. It has introduced many new APIs for Comparator. Lets go through some new feature of Comparator using Lambda expression.
Lets create a very simple class called Employee:
package org.arpit.java2blog;

public class Employee {

 private String employeeName;
 private int age;

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

 }

 public String getEmployeeName() {
  return employeeName;
 }

 public void setEmployeeName(String employeeName) {
  this.employeeName= employeeName;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String toString()
 {
  return getEmployeeName()+" : "+getAge();
 }
}

Sort Employee list by name in classic way:

We will sort employee list in classic way
Collections.sort(employeeList,new Comparator<Employee>() {
   public int compare(Employee e1,Employee e2)
   {
    return e1.getName().compareTo(e2.getName());
   }
   
  });
Create EmployeeMain.java
package org.arpit.java2blog;

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

public class EmployeeMain {

 public static void main(String[] args)
 {
  List<Employee> employeeList=getEmployees();
  System.out.println("Before Sorting: ");
  System.out.println(employeeList);
                // in classic way
  Collections.sort(employeeList,new Comparator<Employee>() {
   public int compare(Employee e1,Employee e2)
   {
    return e1.getName().compareTo(e2.getName());
   }
   
  });
                System.out.println("After Sorting: ");
                System.out.println(employeeList);
 }
 
 public static List<Employee> getEmployees()
 {
  List<Employee> employeesList=new ArrayList<Employee>();
  Employee e1=new Employee("John", 35);
  Employee e2=new Employee("Adam", 22);
  Employee e3=new Employee("Arpit", 28);
  Employee e4=new Employee("John", 30);
  Employee e5=new Employee("Grace", 38);
  Employee e6=new Employee("Arpit", 25);
  employeesList.add(e1);
  employeesList.add(e2);
  employeesList.add(e3);
  employeesList.add(e4);
  employeesList.add(e5);
  employeesList.add(e6);
  return employeesList;

 }
}

Run the above program and you will get following output:
Before Sorting: 
[John : 35, Adam : 22, Arpit : 28, John : 30, Grace : 38, Arpit : 25]
After Sorting: 
[Adam : 22, Arpit : 28, Arpit : 25, Grace : 38, John : 35, John : 30]

Sort Employee list by name using lambda expression:

We will sort Employee list by name using lambda expression, It will reduce complex Comparator syntax to simple line of code
Create EmployeeMain.java
package org.arpit.java2blog;

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

public class EmployeeMain {

 public static void main(String[] args)
 {
  List<Employee> employeeList=getEmployees();
  System.out.println("Before Sorting: ");
  System.out.println(employeeList);
  // using lambda expression
  Collections.sort(employeeList,(Employee e1,Employee e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName())); 
               System.out.println("After Sorting: "); 
          System.out.println(employeeList);
 }
 
 public static List<Employee> getEmployees()
 {
  List<Employee> employeesList=new ArrayList<Employee>();
  Employee e1=new Employee("John", 35);
  Employee e2=new Employee("Adam", 22);
  Employee e3=new Employee("Arpit", 28);
  Employee e4=new Employee("John", 30);
  Employee e5=new Employee("Grace", 38);
  Employee e6=new Employee("Arpit", 25);
  employeesList.add(e1);
  employeesList.add(e2);
  employeesList.add(e3);
  employeesList.add(e4);
  employeesList.add(e5);
  employeesList.add(e6);
  return employeesList;

 }
}

Run the above program and you will get following output:
Before Sorting: 
[John : 35, Adam : 22, Arpit : 28, John : 30, Grace : 38, Arpit : 25]
After Sorting: 
[Adam : 22, Arpit : 28, Arpit : 25, Grace : 38, John : 35, John : 30]]

Sort Employee list by name using lambda expression without type definitions:

Here we will not pass type definition to arguments in lambda expression.It will be interpreted in context
Just  change line no. 16 of Employee name from :
Collections.sort(employeeList,(Employee e1,Employee e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName()));
To
Collections.sort(employeeList,(e1,e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName()));
and Run EmployeeMain.java. You will get same output as above

Reverse sort:

JDK 8 has introduced a new help method for reverse sorting a list.
Change the main method in employee main to below method:
 public static void main(String[] args)
 {
  List<Employee> employeeList=getEmployees();
  System.out.println("Before Sorting: ");
  System.out.println(employeeList);
  // using lambda expression
  Collections.sort(employeeList,(Employee e1,Employee e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName())); 
             System.out.println("After Sorting: ");  
               System.out.println(employeeList);
 }

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012