Java-1207
创始人
2024-03-25 16:49:10
0

Stream API

  1. 流的概念:

    1. 是数据渠道,用于操作数据源所生成的元素序列。集合讲的是数据,流讲的是计算
    2. 注意
      1. Stream自己不会存储元素
      2. Stream不会改变源对象。相反,他们会返回一个持有结果的新的Stream
      3. Stream操作是延迟执行的。意味着他们会等到需要结果的时候才会执行。
  2. 步骤

    1. 创建Stream
    2. 中间操作
    3. 终止操作
  3. 创建stream

    1. 可以通过collection系列集合提供的stream()或parallelStream()方法获取,第一个是串行流,第二个是并行流。

      Listlist = new ArrayList<>();
      Stream stream = list.stream();
      
    2. 通过Arrays中的静态方法stream()获取数组流

      Listlist = new ArrayList<>();
      Stream stream = list.stream();
      
    3. 通过Stream类中的静态方法of()

      Stream stringStream = Stream.of("aa", "bb", "cc");
      
    4. 创建无限流

      //  迭代
      Stream integerStream = Stream.iterate(0, (x) -> x + 2);//  生成
      Stream doubleStream = Stream.generate(Math::random);
      
  4. 中间操作:

    1. 惰性求值:多个中间操作连接成一个流水线,除非流水线触发终止操作,否则中间操作不会执行任何处理。在终止操作时一次性全部处理。

    2. 筛选与切片操作

      1. Filter:接收lambda,从流中排除某些元素

        1. 内部迭代:

        2. 惰性求值:

        3. filter(x -> x.getAge() > 10)
          
      2. limit:截断流,使其元素不超过给定数量

        1. 内部会先找到满足条件的前n个值,内部迭代操作也仅仅在这几个值进行,我们称之为短路

          employees.stream().filter(e->{System.out.println("limit");return  e.getSalary() > 5000;}).limit(5).forEach(System.out::println);
          
      3. skip:跳过元素,返回一个扔掉了前n个元素的流。若流中元素不足n个,则返回一个空流。与limit互补

      4. distinct:筛选,通过流所生成元素的hashcode( )和equals()去除重复元素

        1. 如果需要对Object进行去重,需要重写hashCode和equals方法
    3. 映射

      1. map(Function f):

        1. 接收Lambda,将元素转化为其他形式或提取信息。接收一个函数作为参数,该函数会被应用到每一个元素上,并将其映射成一个新的元素。
        2. map操作返回的依然是流
        3. 经过map操作后的流的元素会变,根据map中Lambda的返回值变化。
      2. mapToDouble(ToDoubleFunction f)

      3. mapToInt(ToIntFunction f)

      4. mapToLong(ToLongFunction f)

      5. flatMap(Function f):接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。

      6. flatMap和Map的区别

        1. 二者的区别类似于add和addAll的区别,前者如果使用list2.add(list),结果会是将整个list作为一个元素放进list2,而list2.addAll(list),是将list里面的每个元素拆分开,以元素的方式存进去。
        public static Stream filterCharacter(String str){Listlist = new ArrayList<>();for(Character ch:str.toCharArray()){list.add(ch);}return list.stream();}@Testpublic void test6(){List list = Arrays.asList("aaa","bbb","ccc","ddd","eee");//  用map,返回Stream>Stream> streamStream = list.stream().map(TestStreamAPI2::filterCharacter);//  用flatMap,返回StreamStream characterStream = list.stream().flatMap(TestStreamAPI2::filterCharacter);}
        
    4. 排序

      1. sorted():自然排序,按照对象指定的Comparable排序

      2. sorted(Comparator comp):定制排序Comparator

        employees.stream().sorted((e1,e2)->{if(e1.getAge()!=e2.getAge()){return -e1.getAge()+e2.getAge();}else return e1.getName().compareTo(e2.getName());}).forEach(System.out::println);
        
  5. 终止操作

    1. 查找与匹配

      1. allMatch():检查是否匹配所有元素,相当于全称量词

        boolean b = employees.stream().allMatch(e -> e.getStatus().equals(Status.BUSY));
        
      2. anyMatch():是否存在一个匹配值,相当于存在量词

      3. noneMatch():检查是否没有匹配的元素,相当于存在量词的非

      4. findFirst():找到第一个元素,返回optional对象

        1. Optional对象:一个容器类,为了避免空指针异常,当返回的值有可能为空时,就会封装进optional

        2. orElse方法:如果当前容器封装的对象指针为空,可以搞一个替代的对象

          Optional first = employees.stream().sorted(Comparator.comparingDouble(Employee::getSalary)).findFirst();first.orElse(new Employee());
          
      5. findAny():找到任意一个元素

      6. count:返回流中元素总数

        long count = employees.stream().count();
        
      7. min:返回流中最小值

        Optional min = employees.stream().map(Employee::getSalary).min(Double::compare);System.out.println(min.get());
        
      8. max:返回流中的最大值

    2. 归约 reduce(T identity, Binaryoperator) / reduce(BinaryOperator) 可以将流中元素反复结合起来,得到一个值。

      1. reduce(T identity, Binaryoperator) : 第一个参数为启始值,第二个参数为运算式

      2. map-reduce 连用成为map-reduce模式:

        Optional map-reduce = employees.stream().map(Employee::getSalary).reduce(Double::sum);System.out.println();
        
    3. 收集 collect-将流转换为其他形式。接收一个collector接口的实现,用于Stream中元素汇总方法

      1. collect(Collector c )

        //  使用listList collect = employees.stream().map(Employee::getName).collect(Collectors.toList());collect.forEach(System.out::println);//  使用setSet set = employees.stream().map(Employee::getName).collect(Collectors.toSet());set.forEach(System.out::println);//  使用构造HashSet stringHashSet = employees.stream().map(Employee::getName).collect(Collectors.toCollection(HashSet::new));
        
      2. Collector.counting:收集总数

        Long aLong = employees.stream().collect(Collectors.counting());
        
      3. Collector.avg : 平均值

        Double avg = employees.stream().collect(Collectors.averagingDouble(Employee::getSalary));
        
      4. Collector.summing :总和

        Double sum = employees.stream().collect(Collectors.summingDouble(Employee::getSalary));
        
      5. 最大值:

        Optional max = employees.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)));
        
      6. 最小值

         Optional min = employees.stream().map(e -> e.getSalary()).collect(Collectors.minBy(Double::compareTo));//	或者
        Optional min = employees.stream().map(Employee::getSalary).min(Double::compareTo);
        
      7. 分组:

        Map> statusListMap = employees.stream().collect(Collectors.groupingBy(Employee::getStatus));
        
      8. 多级分组

        Map>> statusMapMap = employees.stream().collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy(e -> {if (e.getAge() > 50) return "老年";else if (e.getAge() <= 50 && e.getAge() > 30) return "中年";else return "青年";})));
        
      9. 分区

        Map> listMap = employees.stream().collect(Collectors.partitioningBy(e -> e.getSalary() > 8000));
        
      10. 统计

        DoubleSummaryStatistics collect = employees.stream().collect(Collectors.summarizingDouble(Employee::getSalary));System.out.println(collect.getAverage());System.out.println(collect.getMax());System.out.println(collect.getSum());
        
      11. 字符串连接

        String collectStr = employees.stream().map(Employee::getName).collect(Collectors.joining(","));
        

相关内容

热门资讯

每周股票复盘:瀚川智能(688... 截至2025年12月26日收盘,瀚川智能(688022)报收于15.3元,较上周的14.42元上涨6...
每周股票复盘:中粮糖业(600... 截至2025年12月26日收盘,中粮糖业(600737)报收于17.27元,较上周的17.18元上涨...
每周股票复盘:马钢股份(600... 截至2025年12月26日收盘,马钢股份(600808)报收于4.22元,较上周的3.82元上涨10...
每周股票复盘:内蒙一机(600... 截至2025年12月26日收盘,内蒙一机(600967)报收于16.34元,较上周的16.05元上涨...
富达基金投顾业务负责人戴旻:封... 由三亚市人民政府主办,《财经》杂志、财经网、《财经智库》、三亚中央商务区管理局、三亚经济研究院承办的...
海南大谷国际园区董事长张焱:以... 由三亚市人民政府主办,《财经》杂志、财经网、《财经智库》、三亚中央商务区管理局、三亚经济研究院承办的...
每周股票复盘:汉马科技(600... 截至2025年12月26日收盘,汉马科技(600375)报收于5.87元,较上周的5.92元下跌0....
每周股票复盘:中天科技(600... 截至2025年12月26日收盘,中天科技(600522)报收于18.4元,较上周的18.37元上涨0...
网约配送员权益维护有了专门法规... 今年厦门市出台《厦门经济特区网约配送员劳动权益保障若干规定》(以下简称《若干规定》),这是厦门市在全...
“十四五”的大冶答卷:政策赋能... 从商业综合体崛起到乡村直播间走红,从政策红利直达市场主体到“大冶制造”扬帆出海……“十四五”期间,大...