Java 8 Lamba Expression examples using Comparator

Previous
Next
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:

Sort Employee list by name in classic way:

We will sort employee list in classic way

Create EmployeeMain.java

Run the above program and you will get following output:

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

Run the above program and you will get following output:

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 :

To

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:

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog