Java 8 — Lambda Expression With Collections | Code Factory

(1) List (I) :

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);
}
}
[one, two, three, four]

(2) Set (I) :

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);
}
}
[one, two, three]

(3) Map (I) :

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);
}
}
{1=one, 3=three, 4=four, 22=twenty two}

Sorted Collections :

(1) Sorted List :

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);
}
}
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);
}
}
Before Sorting : [2, 3, 1, 4]
After Sorting : [4, 3, 2, 1]

Sorting with Lambda Expressions :

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);
}
}
Before Sorting : [2, 3, 1, 4]
After Sorting : [4, 3, 2, 1]

(2) Sorted Set :

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);
}
}
[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);
}
}
[4, 3, 2, 1, 0]

(3) Sorted Map :

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store