5-常用流的中间操作和终端操作

常用流的中间操作

filter使用:过滤掉不符合断言判断的数据
map使用:将一个元素转换成另一个元素
flatMap使用:将一个对象转换成流
peek使用:遍历操作
sorted使用:排序
distinct使用:去重
skip使用:跳过前几条

limit使用:取限制条数数据

常用流的终端操作

allMatch使用:所有元素匹配返回true
anyMatch使用:任何元素匹配返回true
noneMatch使用:任何元素都不匹配返回true
findFirst使用:找到第一个
findAny使用:找到任意一个
max使用:获取最大值
min使用:获取最小值
count使用:获取集合个数
  1 import com.alibaba.fastjson.JSON;
  2 import org.junit.Before;
  3 import org.junit.Test;
  4 
  5 import java.util.*;
  6 
  7 /**
  8  * @description: 演示流的各种操作
  9  */
 10 public class StreamOperator {
 11 
 12     static class Student{
 13         private String name;
 14         private Integer age;
 15 
 16         Student(String name, Integer age){
 17             this.name = name;
 18             this.age = age;
 19         }
 20 
 21         public String getName() {
 22             return name;
 23         }
 24 
 25         public void setName(String name) {
 26             this.name = name;
 27         }
 28 
 29         public Integer getAge() {
 30             return age;
 31         }
 32 
 33         public void setAge(Integer age) {
 34             this.age = age;
 35         }
 36     }
 37 
 38     List<Student> list;
 39 
 40     @Before
 41     public void init(){
 42         /**
 43          * 该写法有内存泄漏风险,不要在生产使用
 44          */
 45         list = new ArrayList<Student>(){
 46             {
 47                 add(new Student("小明", 22));
 48                 add(new Student("小黄", 20));
 49                 add(new Student("小红", 21));
 50                 add(new Student("小超", 25));
 51                 add(new Student("小王", 20));
 52             }
 53         };
 54     }
 55 
 56     /**
 57      * filter使用:过滤掉不符合断言判断的数据
 58      */
 59     @Test
 60     public void filterTest(){
 61         list.stream()
 62                 .filter(student -> student.getAge() > 20)
 63                 .forEach(item ->
 64                         System.out.println(
 65                                 JSON.toJSONString(item, true)
 66                         ));
 67         //{
 68         //    "age":22,
 69         //    "name":"小明"
 70         //}
 71         //{
 72         //    "age":21,
 73         //    "name":"小红"
 74         //}
 75         //{
 76         //    "age":25,
 77         //    "name":"小超"
 78         //}
 79     }
 80 
 81     /**
 82      * 将一个元素转换成另一个元素
 83      */
 84     @Test
 85     public void mapTest(){
 86         list.stream()
 87                 .map(student -> student.getName())
 88                 .forEach(item ->
 89                         System.out.print(
 90                                 JSON.toJSONString(item, true)
 91                         ));
 92         //"小明""小黄""小红""小超""小王"
 93     }
 94 
 95     /**
 96      * flatMap使用:将一个对象转换成流
 97      */
 98     @Test
 99     public void flatMapTest(){
100         list.stream()
101                 .flatMap(student -> Arrays.stream(
102                         student.getName().split("")
103                 ))
104                 .forEach(item ->
105                         System.out.print(
106                                 JSON.toJSONString(item, true)
107                         ));
108         //"小""明""小""黄""小""红""小""超""小""王"
109     }
110 
111     /**
112      * peek使用:遍历操作
113      * 注意:此时peek和forEach是交替打印的
114      */
115     @Test
116     public void peekTest(){
117         list.stream()
118                 .peek(student -> System.out.println(student.getName()))
119                 .forEach(item ->
120                         System.out.println(
121                                 JSON.toJSONString(item, true)
122                         ));
123         //小明
124         //{
125         //    "age":22,
126         //    "name":"小明"
127         //}
128         //小黄
129         //{
130         //    "age":20,
131         //    "name":"小黄"
132         //}
133         //小红
134         //{
135         //    "age":21,
136         //    "name":"小红"
137         //}
138         //小超
139         //{
140         //    "age":25,
141         //    "name":"小超"
142         //}
143         //小王
144         //{
145         //    "age":20,
146         //    "name":"小王"
147         //}
148     }
149 
150     /**
151      * sorted使用:排序
152      * 注意:添加有状态的sorted操作后,peek和forEach分开打印
153      */
154     @Test
155     public void sortTest(){
156         list.stream()
157                 .peek(student -> System.out.println(student.getName()))
158                 .sorted(Comparator.comparing(Student::getAge).reversed())
159                 .forEach(item ->
160                         System.out.println(
161                                 JSON.toJSONString(item, true)
162                         ));
163         //小明
164         //小黄
165         //小红
166         //小超
167         //小王
168         //{
169         //    "age":20,
170         //    "name":"小黄"
171         //}
172         //{
173         //    "age":20,
174         //    "name":"小王"
175         //}
176         //{
177         //    "age":21,
178         //    "name":"小红"
179         //}
180         //{
181         //    "age":22,
182         //    "name":"小明"
183         //}
184         //{
185         //    "age":25,
186         //    "name":"小超"
187         //}
188     }
189 
190     /**
191      * distinct使用:去重
192      */
193     @Test
194     public void distinctTest(){
195         list.stream()
196                 .map(student -> student.getAge())
197                 .distinct()
198                 .forEach(item ->
199                         System.out.print(
200                                 JSON.toJSONString(item, true) + " "
201                         ));
202         //22 20 21 25
203     }
204 
205     /**
206      * skip使用:跳过前几条
207      */
208     @Test
209     public void skipTest(){
210         list.stream()
211                 .sorted(Comparator.comparing(Student::getAge))
212                 .skip(2)
213                 .forEach(item ->
214                         System.out.println(
215                                 JSON.toJSONString(item, true)
216                         ));
217         //{
218         //    "age":21,
219         //    "name":"小红"
220         //}
221         //{
222         //    "age":22,
223         //    "name":"小明"
224         //}
225         //{
226         //    "age":25,
227         //    "name":"小超"
228         //}
229     }
230 
231     /**
232      * limit使用:取限制条数数据
233      * skip和limit可以实现伪分页
234      */
235     @Test
236     public void limitTest(){
237         list.stream()
238                 .sorted(Comparator.comparing(Student::getAge))
239                 .limit(3)
240                 .forEach(item ->
241                         System.out.println(
242                                 JSON.toJSONString(item, true)
243                         ));
244         //{
245         //    "age":20,
246         //    "name":"小黄"
247         //}
248         //{
249         //    "age":20,
250         //    "name":"小王"
251         //}
252         //{
253         //    "age":21,
254         //    "name":"小红"
255         //}
256     }
257 
258     /**
259      * allMatch使用:所有元素匹配返回true
260      * 终端操作,短路操作,只要不满足就立刻返回
261      */
262     @Test
263     public void allMatchTest() {
264         boolean match = list.stream()
265                 .peek(student -> System.out.println(student.getName()))
266                 .allMatch(student -> student.getAge() > 20);
267         System.out.println(match);
268         //小明
269         //小黄
270         //false
271     }
272 
273     /**
274      * anyMatch使用:任何元素匹配返回true
275      * 终端操作,短路操作,只要满足就立刻返回
276      */
277     @Test
278     public void anyMatchTest() {
279         boolean match = list.stream()
280                 .peek(student -> System.out.println(student.getName()))
281                 .anyMatch(student -> student.getAge() > 20);
282         System.out.println(match);
283         //小明
284         //true
285     }
286 
287     /**
288      * noneMatch使用:任何元素都不匹配返回true
289      */
290     @Test
291     public void noneMatchTest() {
292         boolean match = list.stream()
293                 .peek(student -> System.out.println(student.getName()))
294                 .noneMatch(student -> student.getAge() > 20);
295         System.out.println(match);
296         //小明
297         //false
298     }
299 
300     /**
301      * findFirst使用:找到第一个
302      */
303     @Test
304     public void findFirstTest(){
305         Optional<Student> optional = list.stream()
306                 .findFirst();
307         System.out.println(JSON.toJSONString(optional.get(), true));
308         //{
309         //    "age":22,
310         //    "name":"小明"
311         //}
312     }
313 
314     /**
315      * findAny使用:找到任意一个
316      */
317     @Test
318     public void findAnyTest(){
319         Optional<Student> optional = list.stream()
320                 .findAny();
321         System.out.println(JSON.toJSONString(optional.get(), true));
322         //{
323         //    "age":22,
324         //    "name":"小明"
325         //}
326     }
327 
328     /**
329      * max使用:获取最大值
330      */
331     @Test
332     public void maxTest(){
333         OptionalInt optionalInt = list.stream()
334                 .mapToInt(Student::getAge)
335                 .max();
336         System.out.println(optionalInt.getAsInt());
337         //25
338     }
339 
340     /**
341      * min使用:获取最小值
342      */
343     @Test
344     public void minTest(){
345         OptionalInt optionalInt = list.stream()
346                 .mapToInt(Student::getAge)
347                 .min();
348         System.out.println(optionalInt.getAsInt());
349         //20
350     }
351 
352     /**
353      * count使用:获取集合个数
354      */
355     @Test
356     public void countTest(){
357         long count = list.stream()
358                 .count();
359         System.out.println(count);
360         //5
361     }
362 }

 


posted @ 2020-06-09 17:27  mingmingn  阅读(379)  评论(0)    收藏  举报