Java Lambda Expression with Collections

In this article Lambda Expression with Collections are 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 contain one abstract method: – compare(). An interface that only contain only a single abstract method then it is called as Functional Interface.

  • Use of Comparator(I): –
  • To define our own sorting, i.e, our own customized sorting then we should use comparator concept.

  • Prototype of compare() method: –
  • compare() method takes two objects as its argument.

    public int compare(Object obj1, Object obj2)
    

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

    compare() method working: –

  • returns negative value(-1), if and only if obj1 has to come before obj2.
  • returns positive value(+1), if and only if obj1 has to come after obj2.
  • returns zero(0), if and only if obj1 and obj2 are equal.

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 at 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Sorting TreeSet using Lambda Expression: –

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.