zoukankan      html  css  js  c++  java
  • 【JDK8】Java8 Stream流API常用操作

    Java版本现在已经发布到JDK13了,目前公司还是用的JDK8,还是有必要了解一些JDK8的新特性的,例如优雅判空的Optional类,操作集合的Stream流,函数式编程等等;这里就按操作例举一些常用的Stream流操作;

    Stream流简介

    A sequence of elements supporting sequential and parallel aggregate operations. Stream流是一个来自数据源的元素队列并支持聚合操作

    Stream流中常用方法的分类

    • 1.中间操作
    • 2.终止操作

    Stream的常用中间操作

    • 1.peek()--消费流中的每一个元素
     List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
     list.stream().peek(i -> System.out.println(i)).forEach(i-> System.out.println(i));
    • 2.map() mapToInt() mapToDouble()等等--将流中的每一个元素映射为另一个元素
    int[] nums={1,2,3,5,6};
    int sum = IntStream.of(nums).map(i->i*2).sum();
    • 3.filter()--将流中满足要求的元素过滤出来
     List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
     list.stream().filter(integer -> integer>1).forEach(System.out::println);
    • 4.sort()--将流中的元素按一定的规则排序
    List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
    List<Integer> collect = list.stream().sorted((o1, o2) -> o1 - o2).collect(Collectors.toList());
    • 5.distinct()--去重
     List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
     list.stream().dinstinct().forEach(System.out::println)
    • 6.limit()--截断流只保留指定个数的元素
    List<Integer> list = Arrays.asList(6, 5, 5, 1, 4);
    list.stream().limit(1).forEach(System.out::println);

    Stream的常用的终止操作

    • 1.count()-获取流中元素的个数
    List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
    long count = list.stream().count();
    • 2.reduce()-将流中所有的元素合并为一个元素,可用于累加求和,求乘积等等
    List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
    Integer reduce = list.stream().reduce((integer, integer2) -> integer + integer2).get();
    • 3.forEach()-遍历流中的所有元素
    List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
    list.stream().forEach(System.out::println);
    • 4.max()-求出流中元素的最大值
    List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
    Integer max = list.stream().max((s1, s2) -> s1 - s2).get();
    • 5.min()-求出流中元素的最小值
     List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
     Integer min = list.stream().max((o1, o2) -> o2 - o1).get();
    • 6.anyMaych()/allMatch()/noneMatch()--匹配元素中的值,返回boolean类型
        List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
        boolean b = list.stream().noneMatch((x) -> x.intValue() == 1);
        boolean b = list.stream().allMatch((x) -> x.intValue() == 1);
        boolean b = list.stream().anyMatch((x) -> x.intValue() == 1);
    • 7.collect()--收集流中的元素通过Collectors这个工具类处理
        List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
        //求平均数
        Double average= list.stream().collect(Collectors.averagingInt(x -> x.intValue()));
        //求分组
        Double average= list.stream().collect(Collectors.groupinngby(x -> x.intValue()));
        //收集成一个List
        List<Integer> collect = list.stream().sorted((o1, o2) -> o1 - o2).collect(Collectors.toList());
    • 8.summerizing() summerizing中封装了很多计算的方法,例如 求和,求平均数等等
        Double sum_= list.stream().collect(Collectors.summarizingInt(value -> value.intValue())).getAve
  • 相关阅读:
    php 显示文件 与Windows文件名排序一致
    pip3 install uwsgi 报错
    centos7 安装mysql 5.7
    Win7 开始菜单搜索添加快捷方式
    centos7.7 clamav 查杀病毒
    CentOS7.x 默认php版本与php7.4共存
    centos6.5 yum安装redis
    centos6 yum安装mysql 5.6 (完整版)
    解决phpmyadmin出现: Maximum execution time of 300
    Castle Windsor 使MVC Controller能够使用依赖注入
  • 原文地址:https://www.cnblogs.com/july-sunny/p/11657390.html
Copyright © 2011-2022 走看看