File tree Expand file tree Collapse file tree
Modern-Java-Examples/src/com/learn/streams Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ This repository contains the basic & 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
You can’t perform that action at this time.
0 commit comments