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

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

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

    private Integer[] array = {10, 3, 3, 15, 9, 23};
  1. map
    private void map() {
        List 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 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 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
    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
    private void sort() {
        // naturalOrder
        List 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
   private void skip() {
        TreeSet collect = Stream.of(array).skip(3).collect(Collectors.toCollection(TreeSet::new));
        System.out.println("collect = " + collect); // [9, 15, 23]
    }
  1. distinct
    private void distinct() {
        LinkedList collect = Stream.of(array).distinct().collect(Collectors.toCollection(LinkedList::new));
        System.out.println("collect = " + collect); // [10, 3, 15, 9, 23]
    }
  1. sum and count
    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
    private void limit() {
        Set collect = Stream.of(array).limit(3).collect(Collectors.toSet());
        System.out.println("collect = " + collect); // [3, 10]
    }
  1. match
        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
    private void find() {
        Optional 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
    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
    private void peek() {
        LinkedHashSet 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
    private void reduce() {
        Integer reduce = Stream.of(array).reduce(100, Integer::sum);
        System.out.println("reduce = " + reduce); // 163
    }
  1. flatMap
    private void flatMap() {
        List 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
    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
    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 用法示例

你可能感兴趣的:(十五个常用经典的 Java8 Stream API 用法示例)