forked from nsquare-jdzone/java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamFlatMapExample.java
More file actions
119 lines (80 loc) · 3.44 KB
/
StreamFlatMapExample.java
File metadata and controls
119 lines (80 loc) · 3.44 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.javadevzone;
import java.util.*;
import java.util.stream.*;
/**
* Created by java developer zone on 5/21/2017.
*/
public class StreamFlatMapExample {
public void stringArrayToString(){
String[][] data = new String[][]{{"java", "scala"}, {"python"}, {"C", "C++"}};
//Stream<String[]>
Stream<String[]> temp = Arrays.stream(data);
//Stream<String>
Stream<String> langStream = temp.flatMap(x -> Arrays.stream(x));
langStream.forEach(System.out::println);
/*Stream<String> stream = stringStream.filter(x -> "a".equals(x.toString()));
stream.forEach(System.out::println);*/
/*Stream<String> stream = Arrays.stream(data)
.flatMap(x -> Arrays.stream(x))
.filter(x -> "a".equals(x.toString()));*/
}
public void stringSetToString(){
List<Employee> employeeList = Employee.getEmployee();
List<String> collect =
employeeList.stream()
.map(x -> x.getLanguages()) //Stream<Set<String>>
.flatMap(x -> x.stream()) //Stream<String>
.distinct()
.collect(Collectors.toList());
collect.forEach(x -> System.out.println(x));
}
public void stringListToString(){
List<List<String>> stringList = new ArrayList<>();
List<String> jvmLanguage = new ArrayList<>();
jvmLanguage.add("Java");
jvmLanguage.add("Scala");
List<String> otherLanguage = new ArrayList<>();
otherLanguage.add("C++");
otherLanguage.add("Ruby");
stringList.add(jvmLanguage);
stringList.add(otherLanguage);
List<String> collect =
stringList.stream()
.flatMap(x -> x.stream()) //Stream<String>
.collect(Collectors.toList());
collect.forEach(x -> System.out.println(x));
}
public void intToStream(){
int[] intArray = {101, 102, 103};
//1. Stream<int[]>
Stream<int[]> streamArray = Stream.of(intArray);
//2. Stream<int[]> -> flatMap -> IntStream
IntStream intStream = streamArray.flatMapToInt(x -> Arrays.stream(x));
intStream.forEach(x -> System.out.println(x));
}
public void longToStream(){
long[] intArray = {1000001L, 1000002L, 1000003L};
//1. Stream<long[]>
Stream<long[]> streamArray = Stream.of(intArray);
//2. Stream<long[]> -> flatMap -> LongStream
LongStream longStream = streamArray.flatMapToLong(x -> Arrays.stream(x));
longStream.forEach(x -> System.out.println(x));
}
public void doubleToStream(){
double[] intArray = {101.1d, 102.2d, 103.3d};
//1. Stream<double[]>
Stream<double[]> streamArray = Stream.of(intArray);
//2. Stream<double[]> -> flatMap -> DoubleStream
DoubleStream doubleStream = streamArray.flatMapToDouble(x -> Arrays.stream(x));
doubleStream.forEach(x -> System.out.println(x));
}
public static void main(String[] args) {
StreamFlatMapExample streamFlatMapExample = new StreamFlatMapExample();
//streamFlatMapExample.stringArrayToString();
//streamFlatMapExample.stringSetToString();
//streamFlatMapExample.stringListToString();
streamFlatMapExample.intToStream();
streamFlatMapExample.longToStream();
streamFlatMapExample.doubleToStream();
}
}