Skip to content

Commit 075dca3

Browse files
authored
Update README.md
1 parent 6c7dc21 commit 075dca3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,25 @@ List<String> vehicleLenList = vehicles.stream()
8787
.collect(Collectors.toList());
8888
```
8989

90+
-------
91+
92+
#### flatMap()
93+
94+
- Given a collection of objects, `map()` takes one input and returns one output, whereas `flatMap()` takes one input, but returns a stream of objects.
95+
96+
- Example to combine a list of list of objects into single list using `flatMap()`
9097

98+
```
99+
List<Integer> list1 = Arrays.asList(1,2);
100+
List<Integer> list2 = Arrays.asList(3,4);
101+
List<Integer> list3 = Arrays.asList(5,6);
102+
103+
List<List<Integer>> listList = Arrays.asList(list1, list2, list3);
91104
105+
// Combine all lists into single list
106+
List<Integer> combinedList = listList.stream().flatMap(x -> x.stream()).collect(Collectors.toList());
107+
108+
// Output: [1,2,3,4,5,6]
109+
```
92110

93111

0 commit comments

Comments
 (0)