-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapVsFlatMap.java
More file actions
47 lines (37 loc) · 1.36 KB
/
MapVsFlatMap.java
File metadata and controls
47 lines (37 loc) · 1.36 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
package com.interview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MapVsFlatMap {
public static void map() {
// making the array list object
ArrayList<String> fruit = new ArrayList<>();
fruit.add("Apple");
fruit.add("mango");
fruit.add("pineapple");
fruit.add("kiwi");
System.out.println("List of fruit-" + fruit);
// lets use map() to convert list of fruit
List<String> list = fruit.stream().map(String::valueOf).collect(Collectors.toList());
System.out.println("List generated by map-" + list);
}
public static void flatMap() {
// making the arraylist object of List of Integer
List<List<Integer> > number = new ArrayList<>();
// adding the elements to number arraylist
number.add(Arrays.asList(1, 2));
number.add(Arrays.asList(3, 4));
number.add(Arrays.asList(5, 6));
number.add(Arrays.asList(7, 8));
System.out.println("List of list-" + number);
List<Integer> flatList = number.stream()
.flatMap(list->list.stream())
.collect(Collectors.toList());
// printing the list
System.out.println("List generate by flatMap-"+ flatList);
}
public static void main(String[] args) {
flatMap();
}
}