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

一、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:

match:anyMatch、noneMatch、allMatch

 1 package com.cy.java8;
 2 
 3 import java.util.Arrays;
 4 import java.util.stream.Stream;
 5 
 6 public class StreamMatch {
 7 
 8     public static void main(String[] args) {
 9         Stream<Integer> s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
10 
11         //s中是否所有元素都大於0
12         boolean b = s.allMatch(i -> i > 0);
13         System.out.println(b);               //true
14 
15         //只要有一個元素大於6就返回true
16         s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
17         boolean b2 = s.anyMatch(i -> i>6);
18         System.out.println(b2);              //true
19 
20         //沒有一個元素滿足小於1
21         s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
22         boolean b3 = s.noneMatch(i -> i<0);
23         System.out.println(b3);              //true
24     }
25 }

find:

 1 package com.cy.java8;
 2 
 3 import java.util.Arrays;
 4 import java.util.Optional;
 5 import java.util.stream.Stream;
 6 
 7 public class StreamFind {
 8 
 9     public static void main(String[] args) {
10         Stream<Integer> s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
11 
12         //過濾出偶數之後,隨便返回一個
13         Optional<Integer> optional1 = s.filter(i -> i % 2 == 0).findAny();
14         System.out.println(optional1.get());
15 
16         //過濾出大於100的數,任意返回一個,如果有值正常返回,沒值返回-1
17         s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
18         Optional<Integer> optional2 = s.filter(i -> i > 100).findAny();
19         System.out.println(optional2.orElse(-1));
20 
21         //過濾出偶數,找到第一個,如果存在就打印
22         s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
23         Optional<Integer> optional3 = s.filter(i -> i % 2 == 0).findFirst();
24         optional3.ifPresent(System.out::println);
25     }
26 }

打印:

2
-1
2

reduce:

 1 package com.cy.java8;
 2 
 3 import java.util.Arrays;
 4 import java.util.Optional;
 5 import java.util.stream.Stream;
 6 
 7 public class StreamReduce {
 8 
 9     public static void main(String[] args) {
10         //reduce: 聚合的作用
11         Stream<Integer> s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5});
12 
13         //求和,初始值設置爲0
14         Integer result = s.reduce(0, (i, j) -> i + j);
15         System.out.println(result);
16 
17         //求最大值
18         s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5});
19         s.reduce((i, j) -> i > j ? i : j).ifPresent(System.out::println);
20 
21         s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5});
22         s.reduce(Integer::max).ifPresent(System.out::println);
23 
24         //只對裏面的偶數進行相乘,計算結果
25         s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5});
26         Integer result2 = s.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j);
27         Optional.of(result2).ifPresent(System.out::println);
28     }
29 }

打印:

15
5
5
8

 

 

-----

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章