-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMapAndFlatMap.java
More file actions
34 lines (29 loc) · 1.24 KB
/
MapAndFlatMap.java
File metadata and controls
34 lines (29 loc) · 1.24 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 MapAndFlatMap;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MapAndFlatMap {
public static void main(String[] args) {
//MAP EXAMPLE
List<String> list = Arrays.asList("apple", "mango","orange");
final List<String> upperCaseList = list.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("MAP OUTPUT: "+upperCaseList);
//MAP OUTPUT: [APPLE, MANGO, ORANGE]
//FLATMAP EXAMPLE
List<List<String>> nestedList = Arrays.asList(
Arrays.asList("apple","mango"),
Arrays.asList("orange","pineapple"),
Arrays.asList("grapes","kiwi")
);
System.out.println("NESTED LIST OUTPUT: "+nestedList);
// NESTED LIST OUTPUT: [[apple, mango], [orange, pineapple], [grapes, kiwi]]
final List<String> flattenedUpperCaseList = nestedList.stream()
.flatMap(List::stream)
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("FLATMAP OUTPUT: "+flattenedUpperCaseList);
// FLATMAP OUTPUT: [APPLE, MANGO, ORANGE, PINEAPPLE, GRAPES, KIWI]
}
}