zoukankan      html  css  js  c++  java
  • 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

    -----

  • 相关阅读:
    Android设备驱动安装
    昨晚摆乌龙了
    生活
    SQLSTATE=57019
    IBM项目六
    不吸烟了
    清明短假
    『ExtJS』使用中需要注意的一些事(持续更新)
    『Spring.Net』IoC 容器
    『Spring.Net』为什么使用?
  • 原文地址:https://www.cnblogs.com/tenWood/p/11517715.html
Copyright © 2011-2022 走看看