Lambda Expression with Collections

Last updated on February 29th, 2024

In this example, we will Sort ArrayList, TreeSet, and TreeMap with Lambda Expression. Then we have to analyze what will affect Lambda Expression and without Lambda Expression in this example.
A “Comparator” is a predefined functional interface. Because it contains only one abstract method i.e., the “compare()” method.
When we call Comparator’s compare method it will return some integer values:

  • Returns negative number (-1) then the object1 has come before object2
  • Returns positive number (+1) then the object1 has come after object2
  • Returns zero number (0) then the object1 and object2 are equal

Example:

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

class MyComparatorSorting implements Comparator < Integer > {

  @Override

  public int compare(Integer objct1, Integer objct2) {

    return (objct1 < objct2) ? -1 : (objct1 > objct2) ? 1 : 0;

  }

}

public class Test {

  public static void main(String[] args) {

    ArrayList < Integer > itrL = new ArrayList < Integer > ();

    itrL.add(10);

    itrL.add(20);

    itrL.add(5);

    itrL.add(0);

    itrL.add(1);

    itrL.add(22);

    System.out.println("Without Sorting:" + itrL);

    Collections.sort(itrL, new MyComparatorSorting());

    System.out.println("With Sorting:" + itrL);

  }
}

Output: 

Without Sorting:[10, 20, 5, 0, 1, 22]
With Sorting:[0, 1, 5, 10, 20, 22]
  • In the above example, we are creating a separate class to implement the Comparator interface. This creating an extra class is increasing the length of the code in this program.
  • This will be created as an extra “.class” file of an extra class i.e., the “MyComparatorSorting” class.

Example:

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

public class Test {

  public static void main(String[] args) {

    ArrayList < Integer > itrL = new ArrayList < Integer > ();

    itrL.add(10);

    itrL.add(20);

    itrL.add(5);

    itrL.add(0);

    itrL.add(1);

    itrL.add(22);

    System.out.println("Without Sorting:" + itrL);

    //calling Lambda Expression

    Comparator < Integer > comp = (obj1, obj2) -> (obj1 < obj2) ? -1 : (obj1 > obj2) ? 1 : 0;

    Collections.sort(itrL, comp);

    System.out.println("With Sorting:" + itrL);

  }
}

Output: 

Without Sorting:[10, 20, 5, 0, 1, 22]
With Sorting:[0, 1, 5, 10, 20, 22]
  • A “Comparator” is a Functional Interface. We can be implemented that with the Lambda expression. Then our code will be concise and there is no need for an extra class like the above.

Example:

import java.util.TreeSet;

public class Test {

  public static void main(String[] args) {

    TreeSet < Integer > ts = new TreeSet < Integer > ((o1, o2) -> (o1 < o2) ? -1 :

      (o1 > o2) ? 1 : 0);

    ts.add(20);

    ts.add(5);

    ts.add(3);

    ts.add(25);

    ts.add(1);

    System.out.println("Sorting TreeSet with Lambda Expression:" + ts);

  }

}

Output: 

Sorting TreeSet with Lambda Expression:[1, 3, 5, 20, 25]
  • In the example, we are sorting elements of the TreeSet with Lambda expression and our code is concise because there is no extra to implement the Comparator interface.
  • In this example, we are sorting elements of the TreeSet in ascending order.
  • This example is generating only one “.class”  file i.e., “Test.class”.

Example:

import java.util.TreeMap;

public class Test {

  public static void main(String[] args) {

    TreeMap < Integer, String > tm = new TreeMap < Integer, String > ((o1, o2) -> (o1 < o2) ? -1 : (o1 > o2) ? 1 : 0);

    tm.put(1, "Raj");

    tm.put(5, "Anil");

    tm.put(2, "Ramesh");

    tm.put(15, "Chetan");

    tm.put(8, "Gaurav");

    System.out.println("Sorting TreeMap with Lambda Expression:" + tm);

  }

}
Sorting TreeMap with Lambda Expression:{1=Raj, 2=Ramesh, 5=Anil, 8=Gaurav, 15=Chetan}
  • In the example, we are sorting elements of the TreeMap with Lambda expression and our code is concise because there is no extra to implement the Comparator interface.
  • In this example, we are sorting elements of the TreeMap in ascending order.
  • This example is generating only one “.class”  file i.e., “Test.class”.
  • Lambda expression is enabling functional programming in the code of the program.

Conclusion:

This example is explained How to use Lambda Expression in the Collection? Why don’t we need to create a separate class for implementing the Comparator interface? What are the values returned by calling the compare method of the Comparator interface?

Leave a Comment