-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamsJoiningExample.java
59 lines (48 loc) · 1.46 KB
/
StreamsJoiningExample.java
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
48
49
50
51
52
53
54
55
56
57
58
59
package com.learn.streams_terminal;
import com.learn.data.Student;
import com.learn.data.StudentDataBase;
import java.util.stream.Collectors;
/**
* joining() Collector used to concatenates the input elements into a
* {@code String}, in encounter order.
*/
public class StreamsJoiningExample {
public static void main(String[] args) {
System.out.println("joining1 : " + joining1());
System.out.println("joining2 : " + joining2());
System.out.println("joining3 : " + joining3());
}
/**
* <p>
* Concatenate all the names that are in StudentDataBase and return as result.
* </p>
* @return
*/
public static String joining1() {
return StudentDataBase.getAllStudents().stream()
.map(Student::getName)
.collect(Collectors.joining());
}
/**
* <p>
* Concatenate all the names by passing a delimiter.
* </p>
* @return
*/
public static String joining2() {
return StudentDataBase.getAllStudents().stream()
.map(Student::getName)
.collect(Collectors.joining(" "));
}
/**
* <p>
* Concatenate all the names by passing Prefix and Suffix.
* </p>
* @return
*/
public static String joining3() {
return StudentDataBase.getAllStudents().stream()
.map(Student::getName)
.collect(Collectors.joining(" , ", "[ ", " ]"));
}
}