File tree Expand file tree Collapse file tree
Modern-Java-Examples/src/com/learn/streams_terminal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .learn .streams_terminal ;
2+
3+ import com .learn .data .Student ;
4+ import com .learn .data .StudentDataBase ;
5+
6+ import java .util .stream .Collectors ;
7+
8+ public class StreamsJoiningExample {
9+
10+ public static void main (String [] args ) {
11+
12+ System .out .println ("joining1 : " + joining1 ());
13+
14+ System .out .println ("joining2 : " + joining2 ());
15+
16+ System .out .println ("joining3 : " + joining3 ());
17+ }
18+
19+ /**
20+ * <p>
21+ * Concatenate all the names that are in StudentDataBase and return as result.
22+ * </p>
23+ * @return
24+ */
25+ public static String joining1 () {
26+ return StudentDataBase .getAllStudents ().stream ()
27+ .map (Student ::getName )
28+ .collect (Collectors .joining ());
29+ }
30+
31+
32+ /**
33+ * <p>
34+ * Concatenate all the names by passing a delimiter.
35+ * </p>
36+ * @return
37+ */
38+ public static String joining2 () {
39+ return StudentDataBase .getAllStudents ().stream ()
40+ .map (Student ::getName )
41+ .collect (Collectors .joining (" " ));
42+ }
43+
44+ /**
45+ * <p>
46+ * Concatenate all the names by passing Prefix and Suffix.
47+ * </p>
48+ * @return
49+ */
50+ public static String joining3 () {
51+ return StudentDataBase .getAllStudents ().stream ()
52+ .map (Student ::getName )
53+ .collect (Collectors .joining (" , " , "[ " , " ]" ));
54+ }
55+ }
Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ This repository contains the basic & advance level examples related to Java
4242 * [ sum(), max(), min() and average()] ( Modern-Java-Examples/src/com/learn/numericstreams/NumericStreamAggregateExample.java )
4343 * [ boxing, unboxing and mapToInt()] ( Modern-Java-Examples/src/com/learn/numericstreams/NumericStreamsBoxingUnboxingExample.java )
4444 * [ mapToObj(), mapToLong() and mapToDouble()] ( Modern-Java-Examples/src/com/learn/numericstreams/NumericStreamsMapExample.java )
45+ * [ Stream Terminal Operations ] ( Modern-Java-Examples/src/com/learn/streams_terminal )
46+ * [ joining()] ( Modern-Java-Examples/src/com/learn/streams_terminal/StreamsJoiningExample.java )
4547
4648
4749<hr />
You can’t perform that action at this time.
0 commit comments