Java Lambda Expression with Collections

Last Updated : 13 Jul, 2026

Lambda expressions make collection operations simpler by replacing anonymous Comparator classes with concise expressions. They are commonly used for sorting, filtering, and processing Java collections such as List, Set, and Map.

  • Simplify custom sorting by implementing the Comparator interface using lambda expressions.
  • Reduce boilerplate code compared to anonymous classes.
  • Commonly used with Collections.sort(), TreeSet, TreeMap, and the Java Streams API.

Comparator Functional Interface

Comparator is a built-in functional interface used to define custom sorting rules. Since it contains only one abstract method (compare()), it can be implemented using a lambda expression.

  • Returns a negative value if the first object should come before the second.
  • Returns zero if both objects are considered equal.
  • Returns a positive value if the first object should come after the second.

Note: Whenever custom sorting is required in collections such as ArrayList, TreeSet, or TreeMap, the JVM internally invokes the compare() method to determine the ordering of elements.

Syntax of Lambda Expression with Comparator:

(o1, o2) -> {
// comparison logic
}

we can also having another way to compare

(o1, o2) -> o1.compareTo(o2)

Sorting a TreeSet Using Lambda Expression

A custom comparator is supplied to the TreeSet constructor using a lambda expression. The TreeSet automatically stores the elements according to the specified sorting logic.

Java
import java.util.*;

public class Demo {
    public static void main(String[] args)
    {
        ArrayList<Integer> al = new ArrayList<Integer>();
        al.add(205);
        al.add(102);
        al.add(98);
        al.add(275);
        al.add(203);
        System.out.println("Elements of the ArrayList " + 
                              "before sorting : " + al);

        // using lambda expression in place of comparator object
        Collections.sort(al, (o1, o2) -> (o1 > o2) ? -1 :
                                       (o1 < o2) ? 1 : 0);

        System.out.println("Elements of the ArrayList after" + 
                                           " sorting : " + al);
    }
}

Output
Elements of the ArrayList before sorting : [205, 102, 98, 275, 203]
Elements of the ArrayList after sorting : [275, 205, 203, 102, 98]

Sorting a TreeSet Using Lambda Expression

A custom comparator is supplied to the TreeSet constructor using a lambda expression. The TreeSet automatically stores the elements according to the specified sorting logic.

Java
import java.util.*;

public class Demo {
    public static void main(String[] args)
    {
        TreeSet<Integer> h = 
                       new TreeSet<Integer>((o1, o2) -> (o1 > o2) ? 
                                          -1 : (o1 < o2) ? 1 : 0);
        h.add(850);
        h.add(235);
        h.add(1080);
        h.add(15);
        h.add(5);
        System.out.println("Elements of the TreeSet after" + 
                                        " sorting are: " + h);
    }
}

Output
Elements of the TreeSet after sorting are: [1080, 850, 235, 15, 5]

Sorting a TreeMap Using Lambda Expression

TreeMap sorts its entries based on keys. By providing a lambda-based comparator, you can customize the ordering of the keys while the values remain associated with their respective keys.

Java
import java.util.*;

public class Demo {
    public static void main(String[] args)
    {
        TreeMap<Integer, String> m = 
                   new TreeMap<Integer, String>((o1, o2) -> (o1 > o2) ? 
                                               -1 : (o1 < o2) ? 1 : 0);
        m.put(1, "Apple");
        m.put(4, "Mango");
        m.put(5, "Orange");
        m.put(2, "Banana");
        m.put(3, "Grapes");
        System.out.println("Elements of the TreeMap " + 
                             "after sorting are : " + m);
    }
}

Output
Elements of the TreeMap after sorting are : {5=Orange, 4=Mango, 3=Grapes, 2=Banana, 1=Apple}

Reverse Sorting Using Lambda Expression

Instead of writing a separate comparator class, a lambda expression is passed directly to the TreeSet constructor to sort the elements in reverse alphabetical order.

Java
// Use a lambda expression to create a reverse comparator
import java.util.*;

class GFG{
public static void main(String args[]){
 
  // Pass a reverse comparator to TreeSet() via a lambda expression
  TreeSet<String> ts=new TreeSet<String>((aStr,bStr) -> bStr.compareTo(aStr));
  
  // Add elements to the Treeset
  ts.add("A");
  ts.add("B");
  ts.add("C");
  ts.add("D");
  ts.add("E");
  ts.add("F");
  ts.add("G");
 
  //Display the elements .
  for(String element : ts)
    System.out.println(element + "");
  
  System.out.println();
}
}

Output
G
F
E
D
C
B
A
Comment