Java 8 Lambda Expression examples using Comparator

Java 8 Lambda Expression using Comparator
Java 8 has made comparator more powerful using Lambda Expression. It has introduced many new APIs for Comparator.

Let’s go through some new features of the Comparator using Lambda expression.Let’s create a very simple class called Employee:

Sort Employee list by name in classic way:

We will sort employee list in classic way

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Create EmployeeMain.java

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

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 :
To
and Run EmployeeMain.java. You will get same output as above

Reverse sort:

To reverse sort the list, just change order of arguments.
Change the main method in employee main to below method:

Related posts

Error: View e82387dt9p may not exist

import_contacts

You may also like:

Related Posts

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read also: Convert LocalDate to Date in java 💻 Awesome Tech Resources: Looking for ⚒️ […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go […]

  • Convert Stream to List in java
    31 December

    Java Stream to List

    In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList() provides […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; LocalDateTime is an immutable object used […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may think that Stream must be similar to InputStream or OutputStream, but that’s not the case. A Stream represents a sequence of elements supporting sequential and parallel aggregate operations. Stream does not store […]

  • Java Stream List to Map
    26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre [crayon-607a7137c415b487218602/] Create a list of movies and convert with to […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.