Lambda Expressions With Collections PDF
Lambda Expressions With Collections PDF
Lambda Expressions With Collections PDF
in Simple Way
Lambda Expressions
with
Collections
DURGASOFT
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
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) import java.util.ArrayList;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) ArrayList<String> l = new ArrayList<String>();
7) l.add("Sunny");
8) l.add("Bunny");
9) l.add("Chinny");
10) l.add("Sunny");
11) System.out.println(l);
12) }
13) }
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.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
2 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
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) import java.util.HashSet;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) HashSet<String> l = new HashSet<String>();
7) l.add("Sunny");
8) l.add("Bunny");
9) l.add("Chinny");
10) l.add("Sunny");
11) System.out.println(l);
12) }
13) }
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
Eg:
Rollno-->Name
mobilenumber-->address
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
3 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
1) import java.util.HashMap;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) HashMap<String,String> m= new HashMap<String,String>();
7) m.put("A","Apple");
8) m.put("Z","Zebra");
9) m.put("Durga","Java");
10) m.put("B","Boy");
11) m.put("T","Tiger");
12) System.out.println(m);
13) }
14) }
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 sorting order. If we want
sorting for the list then we should use Collections class sort() method.
Instead of Default natural sorting order if we want customized sorting order then we should go for
Comparator interface.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
4 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
1) import java.util.ArrayList;
2) import java.util.Collections;
3) class Test
4) {
5) public static void main(String[] args)
6) {
7) ArrayList<Integer> l = new ArrayList<Integer>();
8) l.add(10);
9) l.add(0);
10) l.add(15);
11) l.add(5);
12) l.add(20);
13) System.out.println("Before Sorting:"+l);
14) Collections.sort(l);
15) System.out.println("After Sorting:"+l);
16) }
17) }
Output:
Before Sorting:[10, 0, 15, 5, 20]
After Sorting:[0, 5, 10, 15, 20]
1) import java.util.TreeSet;
2) import java.util.Comparator;
3) class MyComparator implements Comparator<Integer>
4) {
5) public int compare(Integer I1,Integer I2)
6) {
7) if(I1<I2)
8) {
9) return +1;
10) }
11) else if(I1>I2)
12) {
13) return -1;
14) }
15) else
16) {
17) return 0;
18) }
19) }
20) }
21) class Test
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
5 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
22) {
23) public static void main(String[] args)
24) {
25) TreeSet<Integer> l = new TreeSet<Integer>(new MyComparator());
26) l.add(10);
27) l.add(0);
28) l.add(15);
29) l.add(5);
30) l.add(20);
31) System.out.println(l);
32) }
33) }
Shortcut way:
1) import java.util.ArrayList;
2) import java.util.Comparator;
3) import java.util.Collections;
4) class MyComparator implements Comparator<Integer>
5) {
6) public int compare(Integer I1,Integer I2)
7) {
8) return (I1>I2)?-1:(I1<I2)?1:0;
9) }
10) }
11) class Test
12) {
13) public static void main(String[] args)
14) {
15) ArrayList<Integer> l = new ArrayList<Integer>();
16) l.add(10);
17) l.add(0);
18) l.add(15);
19) l.add(5);
20) l.add(20);
21) System.out.println("Before Sorting:"+l);
22) Collections.sort(l,new MyComparator());
23) System.out.println("After Sorting:"+l);
24) }
25) }
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
6 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
Collections.sort(l,(I1,I2)->(I1<I2)?1:(I1>I2)?-1:0);
1) import java.util.ArrayList;
2) import java.util.Collections;
3) class Test
4) {
5) public static void main(String[] args)
6) {
7) ArrayList<Integer> l= new ArrayList<Integer>();
8) l.add(10);
9) l.add(0);
10) l.add(15);
11) l.add(5);
12) l.add(20);
13) System.out.println("Before Sorting:"+l);
14) Collections.sort(l,(I1,I2)->(I1<I2)?1:(I1>I2)?-1:0);
15) System.out.println("After Sorting:"+l);
16) }
17) }
Output:
Before Sorting:[10, 0, 15, 5, 20]
After Sorting:[20, 15, 10, 5, 0]
2. Sorted Set
In the case of Set, if we want Sorting order then we should go for TreeSet
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
7 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
1) import java.util.TreeSet;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) TreeSet<Integer> t = new TreeSet<Integer>();
7) t.add(10);
8) t.add(0);
9) t.add(15);
10) t.add(5);
11) t.add(20);
12) System.out.println(t);
13) }
14) }
1) import java.util.TreeSet;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) TreeSet<Integer> t = new TreeSet<Integer>((I1,I2)->(I1>I2)?-1:(I1<I2)?1:0);
7) t.add(10);
8) t.add(0);
9) t.add(15);
10) t.add(25);
11) t.add(5);
12) t.add(20);
13) System.out.println(t);
14) }
15) }
3. Sorted Map:
In the case of Map, if we want default natural sorting order of keys then we should go for
TreeMap.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
8 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
1) import java.util.TreeMap;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) TreeMap<Integer,String> m = new TreeMap<Integer,String>();
7) m.put(100,"Durga");
8) m.put(600,"Sunny");
9) m.put(300,"Bunny");
10) m.put(200,"Chinny");
11) m.put(700,"Vinny");
12) m.put(400,"Pinny");
13) System.out.println(m);
14) }
15) }
1) import java.util.TreeMap;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) TreeMap<Integer,String> m = new TreeMap<Integer,String>((I1,I2)->(I1<I2)?1:(I1>I2)?-
1:0);
7) m.put(100,"Durga");
8) m.put(600,"Sunny");
9) m.put(300,"Bunny");
10) m.put(200,"Chinny");
11) m.put(700,"Vinny");
12) m.put(400,"Pinny");
13) System.out.println(m);
14) }
15) }
1) import java.util.ArrayList;
2) import java.util.Collections;
3) class Employee
4) {
5) int eno;
6) String ename;
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
9 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
Output:
Before Sorting:
[100:Katrina, 600:Kareena, 200:Deepika, 400:Sunny, 500:Alia, 300:Mallika]
After Sorting:
[100:Katrina, 200:Deepika, 300:Mallika, 400:Sunny, 500:Alia, 600:Kareena]
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
10 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Java 8 New Features
in Simple Way
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
11 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com