码迷,mamicode.com
首页 > 其他好文 > 详细

Stream之filter、distinct、skip、map、flatMap、match、find、reduce

时间:2019-09-13 22:48:54      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:mon   ack   spl   package   rds   pre   private   port   个数   

一、Stream之filter、distinct、skip:

 1 package com.cy.java8;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.stream.Collectors;
 6 
 7 public class StreamFilter {
 8 
 9     public static void main(String[] args) {
10         List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 8);
11 
12         //取出偶数
13         List<Integer> result = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
14         System.out.println(result);
15 
16         //去重
17         List<Integer> result1 = list.stream().distinct().collect(Collectors.toList());
18         System.out.println(result1);
19 
20         //跳过前面的5个元素
21         List<Integer> result2 = list.stream().skip(5).collect(Collectors.toList());
22         System.out.println(result2);
23 
24         //只要前面的5个
25         List<Integer> result3 = list.stream().limit(5).collect(Collectors.toList());
26         System.out.println(result3);
27     }
28 }

打印结果:

[2, 4, 6, 6, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
[6, 6, 7, 7, 8]
[1, 2, 3, 4, 5]

二、Stream之map、flatMap:  

 1 package com.cy.java8;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.stream.Collectors;
 6 import java.util.stream.Stream;
 7 
 8 public class StreamMap {
 9 
10     public static void main(String[] args) {
11         List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 8);
12 
13         //list集合中每个数放大两倍
14         List<Integer> result1 = list.stream().map(i -> i * 2).collect(Collectors.toList());
15         System.out.println(result1);
16 
17         //只返回Dish中的name
18         List<String> result2 = listDish().stream().map(d -> d.getName()).collect(Collectors.toList());
19         System.out.println(result2);
20 
21         /**
22          * 需求:将Hello和World单词中重复的字母去掉
23          * flatmap flat(扁平化)
24          */
25         String[] words = {"Hello", "World"};
26         //{H,e,l,l,o}, {W,o,r,l,d}
27         Stream<String[]> stream = Arrays.stream(words).map(w -> w.split(""));   //Stream<String[]>
28         //H,e,l,l,o,W,o,r,l,d
29         Stream<String> stringStream = stream.flatMap(Arrays::stream);
30         List<String> result3 = stringStream.distinct().collect(Collectors.toList());
31         System.out.println(result3);
32     }
33 
34     private static List<Dish> listDish(){
35         List<Dish> menu = Arrays.asList(
36                 new Dish("pork", false, 800, Dish.Type.MEAT),
37                 new Dish("beef", false, 700, Dish.Type.MEAT),
38                 new Dish("chicken", false, 400, Dish.Type.MEAT),
39                 new Dish("french fries", true, 530, Dish.Type.OTHER),
40                 new Dish("rice", true, 350, Dish.Type.OTHER),
41                 new Dish("season fruit", true, 120, Dish.Type.OTHER),
42                 new Dish("pizza", true, 550, Dish.Type.OTHER),
43                 new Dish("prawns", false, 300, Dish.Type.FISH),
44                 new Dish("salmon", false, 450, Dish.Type.FISH));
45         return menu;
46     }
47 }

打印结果:

[2, 4, 6, 8, 10, 12, 12, 14, 14, 16]
[pork, beef, chicken, french fries, rice, season fruit, pizza, prawns, salmon]
[H, e, l, o, W, r, d]

 

三、stream之match、find、reduce:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-----

 

Stream之filter、distinct、skip、map、flatMap、match、find、reduce

标签:mon   ack   spl   package   rds   pre   private   port   个数   

原文地址:https://www.cnblogs.com/tenWood/p/11517715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!