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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package 新特性.Stream;

import 新特性.User;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

/**
* @Author: 赵博雅
* @Date: 2020/4/8 1:01
*/
public class Stream入门 {

final static User w = new User("w",10);
final static User x = new User("x",11);
final static User y = new User("y",12);


public static void test1(){

ArrayList<Integer> list = new ArrayList<>();
java.util.stream.Stream.of("abc","bcd","sddfa","sdeergth","hhhghghgh","dd","")
.map(string -> string.length())
.forEach(string -> list.add(string));

System.out.println(list);

}
public static void test2(){

ArrayList<Integer> list = new ArrayList<>();
java.util.stream.Stream.of("abc","bcd","sddfa","sdeergth","hhhghghgh","dd","")
.map(String :: length)
.forEach(string -> list.add(string));
System.out.println(list);
}
public static void test3(){

List<Integer> collect = java.util.stream.Stream.of("abc", "bcd", "sddfa", "sdeergth", "hhhghghgh", "dd", "")
.filter(string -> !string.isEmpty())
.filter(string -> string.length() > 4)
.map(String::length)
.collect(toList());

System.out.println(collect);
}

/**
* mapToInt 将数据流中得元素转成Int,这限定了转换的类型Int,最终产生的流为IntStream,及结果只能转化成int。
*/
public static void test4(){

ArrayList<Integer> list = new ArrayList<>();
java.util.stream.Stream.of("abc","bcd","sddfa","sdeergth","hhhghghgh","dd","")
.mapToInt(String :: length)
.forEach(string -> list.add(string));
System.out.println(list);
}

public static void test5() {

ArrayList<String> list = new ArrayList<>();

java.util.stream.Stream.of("a-b-c-d", "e-f-i-g-h")
.flatMap(e -> java.util.stream.Stream.of(e.split("-")))
.forEach(e -> list.add(e));
System.out.println(list);
}

/**
* limit 限制元素的个数,只需传入 long 类型 表示限制的最大数
*/
public static void test6() {

List<String> collect = java.util.stream.Stream.of("abc", "bcd", "sddfa", "sdeergth", "hhhghghgh", "dd", "")
.limit(4)
.collect(toList());

System.out.println(collect);
}

/**
* distinct 将根据equals 方法进行判断 【去重】
*/
public static void test7() {

List<Integer> collect = java.util.stream.Stream.of(1, 2, 3, 1, 2, 5, 6, 7, 8, 0, 0, 1, 2, 3, 1)
.distinct() //去重
.collect(toList());
System.out.println(collect);
}

/**
* peek 挑选 ,将元素挑选出来,可以理解为提前消费
*/
public static void test8() {

List<User> collect = java.util.stream.Stream.of(w, x, y)
.peek(e -> {if(e.getAge()>11) e.setName(e.getAge() + e.getName());}) //重新设置名字 变成 年龄+名字
.collect(toList());
System.out.println(collect);
}

/**
* skip 跳过 元素
*/
public static void test9() {

List<User> collect = java.util.stream.Stream.of(w, x, y)
.skip(2)//跳过前2个
.peek(e -> e.setName(e.getAge() + e.getName())) //重新设置名字 变成 年龄+名字
.collect(toList());
System.out.println(collect);
}

/**
* sorted 排序 底层依赖Comparable 实现,也可以提供自定义比较器 -> test11
*/
public static void test10() {
List<Integer> collect = new ArrayList<>();
for (Integer integer : Arrays.asList(2, 1, 3, 6, 4, 9, 6, 8, 0)) {
collect.add(integer);
}
collect.sort(null);
System.out.println(collect);
}

/**
* sorted 排序 -> 自定义比较器
*/
public static void test11() {
User x = new User("x",11);
User y = new User("y",12);
User w = new User("w",10);

List<User> collect = java.util.stream.Stream.of(w, x, y)
.sorted((e1,e2) -> e1.getAge()>e2.getAge()?1:e1.getAge()==e2.getAge()?0:-1)
.collect(toList());
System.out.println(collect);
}

/**
* toMap()
*/
public static void test12() {
User x = new User("x",11);
User y = new User("y",12);
User w = new User("w",10);

Map<String, Integer> collect = java.util.stream.Stream.of(w, x, y)
.collect(toMap(User::getName, User::getAge));
System.out.println(collect);
}

/**
* count 统计数据流中的元素个数,返回的是long 类型
*/
public static void test13() {

long count = java.util.stream.Stream.of(1, 2, 3, 1, 2, 5, 6, 7, 8, 0, 0, 1, 2, 3, 1)
.distinct() //去重
.count();
System.out.println(count);
}

/**
* findFirst 获取流中的第一个元素
*/
public static void test14() {

Optional<String> first = java.util.stream.Stream.of("apple", "banana", "orange", "waltermaleon", "grape")
.findFirst();
first.ifPresent(e->System.out.println(e));

}

/**
* findAny 获取流中任意一个元素
*/
public static void test15() {
Optional<String> first = java.util.stream.Stream.of("apple", "banana","waltermaleon", "orange", "grape")
.parallel()
.findAny();
first.ifPresent(e->System.out.println(e));

}

/**
* noneMatch 数据流中得没有一个元素与条件匹配的
*/
public static void test16() {

boolean result = java.util.stream.Stream.of("aa", "bb", "cc", "aa")
.noneMatch(e -> e.equals("dd"));
System.out.println(result);
}

/**
* reduce 是一个规约操作,所有的元素归约成一个,比如对所有元素求和,乘积
*/
public static void test17() {

Integer reduce = java.util.stream.Stream.of(1, 5, 2, 3, 6, 5, 0, -4, 5)
.reduce(0, (e1, e2) -> e1 + e2);
System.out.println(reduce);
}

/**
* forEachOrdered 适用用于并行流的情况下进行迭代,能保证迭代的有序性
*/
public static void test18() {

java.util.stream.Stream.of(1, 5, 2, 3, 6, 5, 0, -4, 5)
.parallel()
.forEachOrdered(e -> System.out.println(Thread.currentThread().getName() +":"+ e));

}

public static void main(String[] args) {

Class c = Stream入门.class;
Stream.of(c.getMethods())
.filter(method -> method.getName().startsWith("test"))
.sorted((method,method2) -> Integer.parseInt(method.getName().replaceAll("test","")) > Integer.parseInt(method2.getName().replaceAll("test","")) ? 0 :-1)
.forEachOrdered(method -> {
try {
System.out.println("-------"+ method.getName()+"-------");
method.invoke(c);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
});
}

}

main函数中的代码充分体现了函数式编程的代码简洁性

result: