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): –
- Prototype of compare() method: –
To define our own sorting, i.e, our own customized sorting then we should use comparator concept.
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.
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); } } |
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: –
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); } } |
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.
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); } } |
Elements of the TreeMap after sorting are : {5=Orange, 4=Mango, 3=Grapes, 2=Banana, 1=Apple}
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between Traditional Collections and Concurrent Collections in java
- Check if a string contains only alphabets in Java using Lambda expression
- Lambda Expressions in Java 8
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.frequency() in Java
- Java.util.Collections.frequency() in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Collections in Java
- Collections.sort() in Java with Examples
- Collections.binarySearch() in Java with Examples
- Collections.reverse() in Java with Examples
- Swapping items of a list in Java : Collections.swap() with Example
- Collections.shuffle() in Java with Examples
- Collections.reverseOrder() in Java with Examples
- Collections.singleton() method in Java with example
- Output of Java programs | Set 13 (Collections)
- Collections checkedMap() method in Java with Examples
- Need of Concurrent Collections in java
- Collections singletonMap() method in Java with Examples
- Collections min() method in Java with Examples
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.

