A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-lambda-expression-with-collections/ below:

Java Lambda Expression with Collections

Java Lambda Expression with Collections

Last Updated : 11 Jul, 2025

In this article, Lambda Expression with Collections is discussed with examples of sorting different collections like ArrayList, TreeSet, TreeMap, etc. Sorting Collections with Comparator (or without Lambda): We can use Comparator interface to sort, It only contains one abstract method: - compare(). An interface that only contains only a single abstract method then it is called a Functional Interface.

While defining our own sorting, JVM is always going to call Comparator to compare() method.

In List, Set, Map, or anywhere else when we want to define our own sorting method, JVM will always call compare() method internally. When there is Functional Interface concept used, then we can use Lambda Expression in its place. Sorting elements of List(I) with Lambda

Expression: - Using lambda expression in place of comparator object for defining our own sorting in collections. 

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);
    }
}

Sorting TreeSet using Lambda Expression:

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);
    }
}

Sorting elements of TreeMap using Lambda Expression: Sorting will be done on the basis of the keys and not its value. 

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);
    }
}

It is also possible to specify a reverse comparator via a lambda expression directly in the call to the TreeSet() constructor, as shown here:

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();
}
}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4