Friday, December 27, 2024
Google search engine
HomeLanguagesJavaJava Lambda Expression with Collections

Java Lambda Expression with Collections

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.

  • Use of Comparator(I): –
  • Prototype of compare() method: –

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

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


Output

G
F
E
D
C
B
A
RELATED ARTICLES

Most Popular

Recent Comments