-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStreamAPI1.java
More file actions
203 lines (160 loc) · 6.03 KB
/
TestStreamAPI1.java
File metadata and controls
203 lines (160 loc) · 6.03 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
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
package Java8Study.stream1;
import Java8Study.lambda1.Employee;
import org.junit.Test;
import java.util.*;
import java.util.stream.Stream;
/**
* 一、Stream操作的三个步骤
*
* - 创建Stream
*
* - 中间操作 : 如果没有终止操作,则不会做任何操作
*
* - 终止操作
*/
public class TestStreamAPI1 {
//1.创建Stream
@Test
public void test1(){
//1.通过Collection 系列集合提供的stream() 或 parallelStream() 获取串行流和并行流
List<String> list = new ArrayList<>();
Stream<String> stream1 = list.stream();
//2.通过Arrays中的静态方法 Stream() 获取数组流
Employee[] employees = new Employee[10];
Stream<Employee> stream2 = Arrays.stream(employees);
//3.通过Stream类中的静态方法 of()创建流
Stream<String> stream3 = Stream.of("aa","bb","cc");
//4.创建无限流
//迭代
Stream<Integer> stream4 = Stream.iterate(0, x -> x+2);
stream4.limit(10).forEach(System.out::println);
//生成
Stream.generate(() -> Math.random()).limit(5).forEach(System.out::println);
}
//2.中间操作
//构造流操作所需数据
List<Employee> employees = Arrays.asList(
new Employee("jerry",18,9999.1, Employee.Status.FREE),
new Employee("tom",23,1234,Employee.Status.BUSY),
new Employee("tom",23,1234, Employee.Status.VACATION),
new Employee("pony",45,12442.1, Employee.Status.FREE),
new Employee("tony",50,1232, Employee.Status.VACATION)
);
/**
* 筛选与切片 filter
*/
@Test
public void test2(){
//中间操作
Stream stream = employees.stream().filter(e -> e.getAge()>35);
/*
注意,这里如果不执行下面的 forEach循环打印操作,上面的中间操作也不会执行!这就是延迟执行或惰性求值
*/
//终止操作
stream.forEach(System.out::println);
}
/**
* 截断流 limit
*/
@Test
public void test3(){
employees.stream().filter(e -> e.getSalary()> 5000).limit(2).forEach(System.out::println);
}
/**
* 跳过元素 skip
*/
@Test
public void test4(){
employees.stream().filter(e -> e.getSalary() > 5000).skip(2).forEach(System.out::println);
}
/**
* 去重 distinct ,通过hashcode 和 equals 进行去重
*/
@Test
public void test5(){
employees.stream().distinct().forEach(System.out::println);
}
/**
* 映射
* map 接收一个函数,把该函数应用到流中每个元素上产生一个新流
* flatMap 接收一个函数,将流中的每个值都换成另一个流,然后把所有流连接成一个流,可以理解为扁平化处理
* Stream<Stream<?>> -----> Stream<?>
*/
@Test
public void test6(){
List<String> list = Arrays.asList("aa","bb","cc","dd");
list.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
employees.stream().map(Employee::getName).forEach(System.out::println);
//注意这里,flatMap和map的区别,flatMap直接将 Steam<Character> 转为了 Character流
//可以与 addAll和 add 进行比较
Stream<Stream<Character>> streamStream = list.stream().map(TestStreamAPI1::toCharacterStream);
Stream<Character> charStream = list.stream().flatMap(TestStreamAPI1::toCharacterStream);
charStream.forEach(System.out::println);
}
//该函数目的是将String转换为Character流
public static Stream<Character> toCharacterStream(String str){
List<Character> list = new ArrayList<>();
for(char c : str.toCharArray()){
list.add(c);
}
return list.stream();
}
/**
* 排序
* sorted
* sorted(Comparator cmp )
*/
@Test
public void test7(){
List<String> list = Arrays.asList("cc","dd","bb","aa");
list.stream().sorted().forEach(System.out::println);
list.stream().sorted(Comparator.comparingInt(s -> s.charAt(1)));
employees.stream().sorted(Comparator.comparingInt(Employee::getAge));
}
//3.终止操作
/**
* 查找与匹配
* allMatch 都匹配
* anyMatch 存在一个匹配
* noneMatch 没有一个匹配
* findFirst 返回第一个
* findAny 返回任意一个
*/
@Test
public void test8(){
boolean b1 = employees.stream().allMatch( e -> e.getStatus().equals(Employee.Status.BUSY));
System.out.println(b1);
boolean b2 = employees.stream().anyMatch( e -> e.getStatus().equals(Employee.Status.BUSY));
System.out.println(b2);
boolean b3 = employees.stream().noneMatch(e -> e.getStatus().equals(Employee.Status.BUSY));
System.out.println(b3);
Optional<Employee> op = employees.stream()
.sorted(Comparator.comparingDouble(Employee::getSalary))
.findFirst();
System.out.println(op.get());
//查找任意个空闲的,先过滤
Optional<Employee> op1 = employees.stream()
.filter(e -> e.getStatus().equals(Employee.Status.FREE))
.findAny();
System.out.println(op1.get());
}
/**
* count 总数
* max 最大值
* min 最小值
*/
@Test
public void test9(){
long count = employees.stream().count();
System.out.println("The sum of employees is : " +count);
//取最大年龄
Optional<Employee> op1 = employees.stream().max(Comparator.comparingInt(Employee::getAge));
System.out.println(op1.get());
//取最低薪水
Optional<Employee> op2 = employees.stream().min(Comparator.comparingDouble(Employee::getSalary));
System.out.println(op2.get());
//获取最小工资
Optional<Double> op3 = employees.stream().map(Employee::getSalary).min(Double::compare);
System.out.println(op3.get());
}
}