前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >十五个常用经典的 Java8 Stream API 用法示例

十五个常用经典的 Java8 Stream API 用法示例

作者头像
happyJared
发布2020-02-18 17:22:11
2K0
发布2020-02-18 17:22:11
举报
文章被收录于专栏:happyJared

不出意外的话,再过几天,Java 11就要正式对外发布了,不知各位同行都用上哪个版本了呢?先贴一张截取的调查图,由此可见,目前Java 7/8的使用群体还是相当之大的。

下面列举了十五个常用经典的Java8 Stream API用法示例。为了方便操作,首先抽取并定义公共数组变量array

代码语言:javascript
复制
    private Integer[] array = {10, 3, 3, 15, 9, 23};
  1. map
代码语言:javascript
复制
    private void map() {
        List<Integer> collect = Stream.of(array).map(n -> n * 2).collect(Collectors.toList());
        System.out.println("collect = " + collect); // [20, 6, 6, 30, 18, 46]

        collect = Stream.of(array).mapToInt(n -> n * 2).boxed().collect(Collectors.toList());
        System.out.println("collect = " + collect); // [20, 6, 6, 30, 18, 46]

        ArrayList<Long> collect1 = Stream.of(array).mapToLong(Integer::longValue).boxed().collect(Collectors.toCollection(ArrayList::new));
        System.out.println("collect1 = " + collect1); // [10, 3, 3, 15, 9, 23]
        
        TreeSet<Object> collect2 = Stream.of(array).mapToDouble(Integer::doubleValue).collect(TreeSet::new, TreeSet::add, TreeSet::addAll);
        System.out.println("collect2 = " + collect2); // [3.0, 9.0, 10.0, 15.0, 23.0]
    }
  1. filter
代码语言:javascript
复制
    private void filter() {
        Object[] objects = Stream.of(array).filter(n -> n >= 10).toArray();
        System.out.println("objects = " + Arrays.toString(objects));// [10, 15, 23]
    }
  1. sort
代码语言:javascript
复制
    private void sort() {
        // naturalOrder
        List<Integer> collect = Stream.of(array).sorted().collect(Collectors.toList());
        System.out.println("collect = " + collect); // [3, 3, 9, 10, 15, 23]

        // reverseOrder
        collect = Stream.of(array).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println("collect = " + collect); // [23, 15, 10, 9, 3, 3]

        collect = Stream.of(array).sorted(Comparator.comparingInt(Integer::intValue).reversed()).collect(Collectors.toList());
        System.out.println("collect = " + collect); // [23, 15, 10, 9, 3, 3]
    }
  1. skip
代码语言:javascript
复制
   private void skip() {
        TreeSet<Integer> collect = Stream.of(array).skip(3).collect(Collectors.toCollection(TreeSet::new));
        System.out.println("collect = " + collect); // [9, 15, 23]
    }
  1. distinct
代码语言:javascript
复制
    private void distinct() {
        LinkedList<Integer> collect = Stream.of(array).distinct().collect(Collectors.toCollection(LinkedList::new));
        System.out.println("collect = " + collect); // [10, 3, 15, 9, 23]
    }
  1. sum and count
代码语言:javascript
复制
    private void sumAndCount() {
        int sum = Stream.of(array).mapToInt(Integer::intValue).sum();
        System.out.println("sum = " + sum); // 63
        
        long sum1 = Stream.of(array).mapToLong(Integer::intValue).sum();
        System.out.println("sum1 = " + sum1); // 63
        
        double sum2 = Stream.of(array).mapToDouble(Integer::intValue).sum();
        System.out.println("sum2 = " + sum2); // 63.0

        // array.length is equal 6
        long count = Stream.of(array).count();
        System.out.println("sumAndCount = " + count); // 6
    }
  1. limit
代码语言:javascript
复制
    private void limit() {
        Set<Integer> collect = Stream.of(array).limit(3).collect(Collectors.toSet());
        System.out.println("collect = " + collect); // [3, 10]
    }
  1. match
代码语言:javascript
复制
        boolean allMatch = Stream.of(array).allMatch(n -> n > 5);
        System.out.println("allMatch = " + allMatch); // false

        boolean anyMatch = Stream.of(array).anyMatch(n -> n > 5);
        System.out.println("anyMatch = " + anyMatch); // true

        boolean noneMatch = Stream.of(array).noneMatch(n -> n > 5);
        System.out.println("noneMatch = " + noneMatch); // false
  1. find
代码语言:javascript
复制
    private void find() {
        Optional<Integer> any = Stream.of(array).filter(n -> n * 2 > 10 && n * 2 < 20).findAny();
        any.ifPresent(n -> System.out.println("any = " + n)); // 9
        
        any = Stream.of(array).filter(n -> n < 5).findFirst();
        any.ifPresent(n -> System.out.println("any = " + n)); // 3
    }
  1. min and max
代码语言:javascript
复制
    private void minAndMax() {
        Stream.of(array).min(Comparator.naturalOrder()).ifPresent(n -> System.out.println("n = " + n)); // 3
        
        Stream.of(array).max(Comparator.comparingInt(Integer::intValue)).ifPresent(n -> System.out.println("n = " + n)); // 23
    }
  1. peek
代码语言:javascript
复制
    private void peek() {
        LinkedHashSet<Integer> collect = Stream.of(array).peek(n -> System.out.print(n + " ")).collect(Collectors.toCollection(LinkedHashSet::new));
        System.out.println();
        System.out.println("collect = " + collect); // [10, 3, 15, 9, 23]
    }
  1. reduce
代码语言:javascript
复制
    private void reduce() {
        Integer reduce = Stream.of(array).reduce(100, Integer::sum);
        System.out.println("reduce = " + reduce); // 163
    }
  1. flatMap
代码语言:javascript
复制
    private void flatMap() {
        List<Integer> collect = Stream.of(array, array).flatMap(Arrays::stream).collect(Collectors.toList());
        System.out.println("collect = " + collect); // [10, 3, 3, 15, 9, 23, 10, 3, 3, 15, 9, 23]
    }
  1. present
代码语言:javascript
复制
    private void present() {
        boolean isPresent = Stream.of(array).findAny().isPresent();
        System.out.println("isPresent = " + isPresent);
        Stream.of(array).filter(n -> n == 20).findFirst().ifPresent(n -> System.out.println("n = " + n));
    }
  1. summaryStatistics
代码语言:javascript
复制
    private void summaryStatistics() {
        IntSummaryStatistics collect = Stream.of(array).collect(Collectors.summarizingInt(Integer::intValue));
        System.out.println(collect.getCount() + " " + collect.getSum() + " " + collect.getMin() + " " + collect.getAverage() + " " + collect.getMax());
        System.out.println("collect = " + collect); // IntSummaryStatistics{sumAndCount=6, sum=63, min=3, average=10.500000, max=23}

        IntSummaryStatistics intSummaryStatistics = Stream.of(array).mapToInt(Integer::intValue).summaryStatistics();
        System.out.println("intSummaryStatistics = " + intSummaryStatistics); // IntSummaryStatistics{sumAndCount=6, sum=63, min=3, average=10.500000, max=23}
    }

文章已授权转载,原文链接:十五个常用经典的 Java8 Stream API 用法示例

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档