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

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

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTimeWas this post helpful? In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You […]

  • Java LocalDate to Instant
    17 February

    Convert LocalDate to Instant in Java

    Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffsetWas this post helpful? In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. […]

  • Java Instant to LocalDate
    17 February

    Convert Instant to LocalDate in Java

    Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8]Was this post helpful? In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns […]

  • Convert String to LocalDateTime in Java
    17 February

    Convert String to LocalDateTime in Java

    Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom formatWas this post helpful? In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. […]

  • Java LocalDateTIme to String
    16 February

    Format LocalDateTime to String in Java

    Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTimeWas this post helpful? In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-62b8f1a63d784296750392/] Here are […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()ConclusionWas this post helpful? Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.