Java 8 — Lambda Expression With Collections | Code Factory

Code Factory
5 min readMay 1, 2020

Index Page : Link

Reference Link : Link

Donate : Link

Collection is nothing but a group of objects represented as a single entity.

The important Collection types are :

  1. List (I)
  2. Set (I)
  3. Map (I)

(1) List (I) :

If we want to represent a group of objects as a single entity where duplicate objects are allowed and insertion order is preserved then we shoud go for List.

  1. Insertion order is preserved
  2. Duplicate objects are allowed

The main implementation classes of List interface are:

  1. ArrayList
  2. LinkedList
  3. Vector
  4. Stack

Demo Program to describe List Properties :

package com.codeFactory.collections;import java.util.ArrayList;public class Test {
public static void main(String... args) {
ArrayList<String> l = new ArrayList<String>();
l.add("one");
l.add("two");
l.add("three");
l.add("four");
System.out.println(l);
}
}

Output :

[one, two, three, four]

Note : List (may be ArrayList, LinkedList, Vector or Stack) never talks about sorting order. If we want sorting for the list then we should use Collections class sort() method.

Collecttions.sort(list) ==> meant for Default Natural Sorting Order

Collections.sort(list, Comparator) ==> meant for Customized Sorting Order

(2) Set (I) :

If we want to represent a group of individual objects as a single entity where duplicate objects are not allowed and insertion order is not preserved then we should go for Set.

  1. Insertion order is not preserved
  2. Duplicates are not allowed.If we are trying to add duplicates then we won’t get any error, just add() method returns false.

The following are important Set implementation classes

  1. HashSet
  2. TreeSet

Demo Program for Set :

package com.codeFactory.collections;import java.util.HashSet;public class Test {
public static void main(String... args) {
HashSet<String> l = new HashSet<String>();
l.add("one");
l.add("two");
l.add("three");
l.add("one");
System.out.println(l);
}
}

Output :

[one, two, three]

Note : In the case of Set, if we want sorting order then we should go for: TreeSet

(3) Map (I) :

If we want to represent objects as key-value pairs then we should go for Map

e.g. : Rollno →Name, mobilenumber →address

The important implementation classes of Map are :

  1. HashMap
  2. TreeMap

Demo Program for Map :

package com.codeFactory.collections;import java.util.HashMap;public class Test {
public static void main(String... args) {
HashMap<Integer, String> l = new HashMap<Integer, String>();
l.put(1, "one");
l.put(22, "twenty two");
l.put(3, "three");
l.put(4, "four");
System.out.println(l);
}
}

Output :

{1=one, 3=three, 4=four, 22=twenty two}

Sorted Collections :

  1. Sorted List
  2. Sorted Set
  3. Sorted Map

(1) Sorted List :

List (may be ArrayList, LinkedList, Vector or Stack) never talks about sortingorder. If we want sorting for the list then we should use Collections class sort() method.

Collecttions.sort(list) ==> meant for Default Natural Sorting Order

For String objects: Alphabetical Order

For Numbers : Ascending order

Instead of Default natural sorting order if we want customized sorting order then we should go for Comparator interface.

Comparator interface contains only one abstract method: compare()

Hence it is Functional interface.

public int compare(obj1, obj2)

returns -ve iff obj1 has to come before obj2

returns +ve iff obj1 has to come after obj2

returns 0 iff obj1 and obj2 are equal

Collections.sort(list, Comparator) ==> meant for Customized Sorting Order

Demo Program to Sort elements of ArrayList according to Defaut Natural Sorting Order (Ascending Order) :

package com.codeFactory.collections;import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String... args) {
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(2);
l.add(3);
l.add(1);
l.add(4);
System.out.println("Before Sorting : " + l);
Collections.sort(l);
System.out.println("After Sorting : " + l);
}
}

Output :

Before Sorting : [2, 3, 1, 4]
After Sorting : [1, 2, 3, 4]

Demo Program to Sort elements of ArrayList according to Customized Sorting Order (Descending Order) :

package com.codeFactory.collections;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class TestComparator implements Comparator<Integer>{@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1; // for Descending
// return o1 - o2; // for Ascending
}
}public class Test {
public static void main(String... args) {
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(2);
l.add(3);
l.add(1);
l.add(4);
System.out.println("Before Sorting : " + l);
Collections.sort(l, new TestComparator());
System.out.println("After Sorting : " + l);
}
}

Output :

Before Sorting : [2, 3, 1, 4]
After Sorting : [4, 3, 2, 1]

Sorting with Lambda Expressions :

As Comparator is Functional interface, we can replace its implementation with Lambda Expression.

Collections.sort(l, (o1, o2) -> o2 — o1);

Demo Program to Sort elements of ArrayList according to Customized Sorting Order By using Lambda Expressions(Descending Order) :

package com.codeFactory.collections;import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String... args) {
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(2);
l.add(3);
l.add(1);
l.add(4);
System.out.println("Before Sorting : " + l);
Collections.sort(l, (o1, o2) -> o2 - o1);
// (o1, o2) -> o2 - o1 // for Descending
// (o1, o2) -> o1 - o2 // for Ascending
System.out.println("After Sorting : " + l);
}
}

Output :

Before Sorting : [2, 3, 1, 4]
After Sorting : [4, 3, 2, 1]

(2) Sorted Set :

In the case of Set, if we want Sorting order then we should go for TreeSet

1. TreeSet t = new TreeSet();

  • This TreeSet object meant for default natural sorting order

2. TreeSet t = new TreeSet(Comparator c);

  • This TreeSet object meant for Customized Sorting Order

Demo Program for Default Natural Sorting Order (Ascending Order) :

package com.codeFactory.collections;import java.util.TreeSet;public class Test {
public static void main(String... args) {
TreeSet<Integer> t = new TreeSet<Integer>();
t.add(2);
t.add(3);
t.add(1);
t.add(4);
t.add(0);
System.out.println(t);
}
}

Output :

[0, 1, 2, 3, 4]

Demo Program for Customized Sorting Order (Descending Order) :

package com.codeFactory.collections;import java.util.TreeSet;public class Test {
public static void main(String... args) {
// (o1, o2) -> o2 - o1 // for Descending
// (o1, o2) -> o1 - o2 // for Ascending
TreeSet<Integer> t = new TreeSet<Integer>((o1, o2) -> o2 - o1);
t.add(2);
t.add(3);
t.add(1);
t.add(4);
t.add(0);
System.out.println(t);
}
}

Output :

[4, 3, 2, 1, 0]

(3) Sorted Map :

In the case of Map, if we want default natural sorting order of keys then we should go for TreeMap.

1. TreeMap m = new TreeMap();

  • This TreeMap object meant for default natural sorting order of keys

2. TreeMap t = new TreeMap(Comparator c);

  • This TreeMap object meant for Customized Sorting Order of keys

Demo Program for Default Natural Sorting Order (Ascending Order) :

package com.codeFactory.collections;import java.util.TreeMap;public class Test {
public static void main(String... args) {
TreeMap<Integer, String> m = new TreeMap<Integer, String>();
m.put(2, "two");
m.put(3, "three");
m.put(1, "one");
m.put(4, "four");
m.put(0, "zero");
System.out.println(m);
}
}

Output :

{0=zero, 1=one, 2=two, 3=three, 4=four}

Demo Program for Customized Sorting Order (Descending Order) :

package com.codeFactory.collections;import java.util.TreeMap;public class Test {
public static void main(String... args) {
// (o1, o2) -> o2 - o1 // for Descending
// (o1, o2) -> o1 - o2 // for Ascending
TreeMap<Integer, String> m = new TreeMap<Integer, String>((o1, o2) -> o2 - o1);
m.put(2, "two");
m.put(3, "three");
m.put(1, "one");
m.put(4, "four");
m.put(0, "zero");
System.out.println(m);
}
}

Output :

{4=four, 3=three, 2=two, 1=one, 0=zero}

Sorting for Customized class objects by using Lambda Expressions :

package com.codeFactory.collections;import java.util.ArrayList;
import java.util.Collections;
class Employee {
int eno;
String ename;

public Employee(int eno, String ename) {
this.eno = eno;
this.ename = ename;
}

@Override
public String toString() {
return "[eno=" + eno + ", ename=" + ename + "]";
}
}
public class Test {
public static void main(String... args) {
ArrayList<Employee> l = new ArrayList<Employee>();
l.add(new Employee(2, "Emp 2"));
l.add(new Employee(3, "Emp 3"));
l.add(new Employee(1, "Emp 1"));
l.add(new Employee(5, "Emp 5"));
l.add(new Employee(4, "Emp 4"));
System.out.println("Before Sorting : " + l);
Collections.sort(l, (e1, e2) -> e1.eno - e2.eno); // for Ascending
// Collections.sort(l, (e1, e2) -> e2.eno - e1.eno); // for Descending
System.out.println("After Sorting : " + l);
}
}

Output :

Before Sorting : [[eno=2, ename=Emp 2], [eno=3, ename=Emp 3], [eno=1, ename=Emp 1], [eno=5, ename=Emp 5], [eno=4, ename=Emp 4]]
After Sorting : [[eno=1, ename=Emp 1], [eno=2, ename=Emp 2], [eno=3, ename=Emp 3], [eno=4, ename=Emp 4], [eno=5, ename=Emp 5]]

--

--