Skip to content

Commit 49c0c42

Browse files
committed
Adding flatMap() Stream Operation
1 parent d987cd8 commit 49c0c42

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.learn.streams;
2+
3+
import com.learn.data.Student;
4+
import com.learn.data.StudentDataBase;
5+
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class StreamsFlatMapExample {
10+
11+
public static void main(String[] args) {
12+
System.out.println(studentActivities());
13+
}
14+
15+
/**
16+
* <p>
17+
* we have to print all activities in a List.
18+
* Using flatMap to Flatten the Stream.
19+
* </p>
20+
* @return
21+
*/
22+
public static List<String> studentActivities() {
23+
24+
return StudentDataBase.getAllStudents().stream()
25+
.map(Student::getActivities)
26+
.flatMap(List::stream)
27+
.collect(Collectors.toList());
28+
29+
}
30+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This repository contains the basic &amp; advance level examples related to Java
2626
* [Collection vs Stream](Modern-Java-Examples/src/com/learn/streams/CollectionVsStream.java)
2727
* [Streams API Operations]
2828
* [map()](Modern-Java-Examples/src/com/learn/streams/StreamsMapExample.java)
29+
* [flatMap()](Modern-Java-Examples/src/com/learn/streams/StreamsFlatMapExample.java)
2930

3031

3132

0 commit comments

Comments
 (0)