-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapVsFlatMap.java
More file actions
85 lines (62 loc) · 2.14 KB
/
MapVsFlatMap.java
File metadata and controls
85 lines (62 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.interview.map;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class EmployeeMap {
private int id;
private String name;
private int salary;
private List<String> phoneNumber;
public EmployeeMap(int id, String name, int salary,List<String> phoneNumber) {
this.id = id;
this.name = name;
this.salary = salary;
this.phoneNumber = phoneNumber;;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public List<String> getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(List<String> phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.id + " " + this.name + " " + this.salary+" "+ phoneNumber;
}
}
public class MapVsFlatMap {
public static void main(String[] args) {
List<EmployeeMap> list = Arrays.asList(new EmployeeMap(1, "Deve1", 2000,Arrays.asList("121","456")),
new EmployeeMap(2, "Deve2", 2000,Arrays.asList("122","457")),
new EmployeeMap(3, "Deve3", 2000,Arrays.asList("123","458")));
// name of all employee
List<String> empName = list.stream().map(emp -> emp.getName()).collect(Collectors.toList());
System.out.println("This is normal map empName :"+empName);
List<List<String>> empPhone = list.stream().map(empsphone -> empsphone.getPhoneNumber()).collect(Collectors.toList());
System.out.println("This is normal map empPhone :"+empPhone);
//flat map
List<String> flat = list.stream().flatMap(phone->phone.getPhoneNumber().stream()).collect(Collectors.toList());
System.out.println("this is flat map value :"+flat);
//flat employee name
List<String> flatEmpname = list.stream().flatMap(empname->empname.getPhoneNumber().stream()).collect(Collectors.toList());
}
}