Table of Contents
Java 8 has made comparator more powerful using Lambda Expression. It has introduced many new APIs for Comparator.
To
and Run EmployeeMain.java. You will get same output as above
Let’s go through some new features of the Comparator using Lambda expression.Let’s create a very simple class called Employee:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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
1 2 3 4 5 6 7 8 9 |
Collections.sort(employeeList,new Comparator() { public int compare(Employee e1,Employee e2) { return e1.getEmployeeName().compareTo(e2.getEmployeeName()); } }); |
Create EmployeeMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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 employeeList=getEmployees(); System.out.println("Before Sorting: "); System.out.println(employeeList); // in classic way Collections.sort(employeeList,new Comparator() { public int compare(Employee e1,Employee e2) { return e1.getEmployeeName().compareTo(e2.getEmployeeName()); } }); System.out.println("After Sorting: "); System.out.println(employeeList); } public static List getEmployees() { List employeesList=new ArrayList(); 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]
[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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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 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 getEmployees() { List employeesList=new ArrayList(); 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]]
[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 :
1 2 3 |
Collections.sort(employeeList,(Employee e1,Employee e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName())); |
1 2 3 |
Collections.sort(employeeList,(e1,e2)->e1.getEmployeeName().compareTo(e2.getEmployeeName())); |
Reverse sort:
To reverse sort the list,
Change the main method in employee main to below method:
just change order of arguments
.Change the main method in employee main to below method:
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void main(String[] args) { List employeeList=getEmployees(); System.out.println("Before Sorting: "); System.out.println(employeeList); // using lambda expression Collections.sort(employeeList,(e1,e2)->e2.getEmployeeName().compareTo(e1.getEmployeeName()));Â System.out.println("After Sorting: "); Â System.out.println(employeeList); } |
Related posts
Error: View e82387dt9p may not exist
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.