forked from Java-Techie-jt/java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapVsFlatMap.java
More file actions
34 lines (28 loc) · 1.35 KB
/
MapVsFlatMap.java
File metadata and controls
34 lines (28 loc) · 1.35 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
package com.javatechie;
import java.util.List;
import java.util.stream.Collectors;
public class MapVsFlatMap {
public static void main(String[] args) {
List<Customer> customers = EkartDataBase.getAll();
//List<Customer> convert List<String> -> Data Transformation
//mapping : customer -> customer.getEmail()
//customer -> customer.getEmail() one to one mapping
List<String> emails = customers.stream()
.map(customer -> customer.getEmail())
.collect(Collectors.toList());
System.out.println(emails);
//customer -> customer.getPhoneNumbers() ->> one to many mapping
//customer -> customer.getPhoneNumbers() ->> one to many mapping
List<List<String>> phoneNumbers = customers.
stream().map(customer -> customer.getPhoneNumbers())
.collect(Collectors.toList());
System.out.println(phoneNumbers);
//List<Customer> convert List<String> -> Data Transformation
//mapping : customer -> phone Numbers
//customer -> customer.getPhoneNumbers() ->> one to many mapping
List<String> phones = customers.stream()
.flatMap(customer -> customer.getPhoneNumbers().stream())
.collect(Collectors.toList());
System.out.println(phones);
}
}