Searching...
Friday, 27 November 2015

Java 8 Comparator Example

22:09
Here is one simple example which is using sorting list of objects using Java 8 Comparator using Lambda expression.

1. Create a java class

package com.vinod.test;

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

public class Java8LambdaComparatorExample {

public static void main(String[] args) {
Employee emp1 = new Employee("Vinod", "Admin", "CA", 33);
Employee emp2 = new Employee("Santhosh", "SD", "CA", 34);
Employee emp3 = new Employee("Anish", "Fin", "CA", 30);
Employee emp4 = new Employee("Raghav", "Sales", "CA", 12);
Employee emp5 = new Employee("Raghav", "Sales", "CA", 12);

List empList = new ArrayList();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
empList.add(emp4);
empList.add(emp5);

List empNewList = new ArrayList();
empNewList.addAll(empList);

// Java 7
System.out.println("Java 7 Sorting using comparator");
Collections.sort(empList, new Comparator() {
public int compare(Employee emp1, Employee emp2) {
return emp1.getName().compareTo(emp2.getName());
}
});

for (Employee e : empList) {
System.out.println(e);
}

// Java 8
System.out.println("Java 8 Sorting using Lambda expression");
Collections.sort(empNewList,
(s1, s2) -> s1.getName().compareTo(s2.getName()));
System.out.println("after sort");
empNewList.stream().forEach(System.out::println);

}

}

2. Output

 

Java 7 Sorting using comparator

Employee [name=Anish, department=Fin, address=CA, age=30]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Employee [name=Santhosh, department=SD, address=CA, age=34]

Employee [name=Vinod, department=Admin, address=CA, age=33]

Java 8 Sorting using Lambda expression

after sort

Employee [name=Anish, department=Fin, address=CA, age=30]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Employee [name=Santhosh, department=SD, address=CA, age=34]

Employee [name=Vinod, department=Admin, address=CA, age=33]

 

0 comments: