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
  • 相关阅读:
    Debian7安装msf
    Debian7配置LAMP(Apache/MySQL/PHP)环境及搭建建站
    五、docker配置镜像加速器之阿里云
    四、harbor实践之初识harbor
    三、harbor部署之SSL
    二、harbor部署之部署harbor
    超级强悍的PHP代码编辑器PHPstorm及配置
    PHP使用DomDocument抓取HTML内容
    37条常用Linux Shell命令组合
    PHP常用数组函数
  • 原文地址:https://www.cnblogs.com/july-sunny/p/11657390.html
Copyright © 2011-2022 走看看