List 转Map

  1. list.stream().collect(Collectors.toMap(SkiiSchedulePlanReportVO::getManagerId, i -> i, (a1, a2) -> a1));

求平均值

  1. List.stream.collect(Collectors.averaginDouble(Double::doubleValue));

filter筛选

  1. List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
  2. Stream<Integer> stream = integerList.stream().filter(i -> i > 3);

distinct去除重复元素

  1. List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
  2. Stream<Integer> stream = integerList.stream().distinct();

limit返回指定流个数

  1. List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
  2. Stream<Integer> stream = integerList.stream().limit(3);

skip跳过流中的元素

  1. List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
  2. Stream<Integer> stream = integerList.stream().skip(2);

元素匹配

提供了三种匹配方式

  • allMatch匹配所有

    1. List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
    2. if (integerList.stream().allMatch(i -> i > 3)) { System.out.println("值都大于3"); }

    通过allMatch方法实现

  • anyMatch匹配其中一个

    1. List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
    2. if (integerList.stream().anyMatch(i -> i > 3)) { System.out.println("存在大于3的值"); }

存在大于3的值则打印,java8中通过anyMatch方法实现这个功能

  • noneMatch全部不匹配

    1. List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
    2. if (integerList.stream().noneMatch(i -> i > 3)) { System.out.println("值都小于3"); }

    查找

  • findFirst查找第一个

    1. List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
    2. Optional<Integer> result = integerList.stream().filter(i -> i > 3).findFirst();
  • findAny随机查找一个

    1. List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
    2. Optional<Integer> result = integerList.stream().filter(i -> i > 3).findAny();

    求和

  • 通过summingInt

    1. int sum = menu.stream().collect(summingInt(Dish::getCalories));
  • 通过reduce

    1. int sum = integerList.stream().reduce(0, Integer::sum);
  • 通过sum

    1. int sum = menu.stream().mapToInt(Dish::getCalories).sum();

reduce接受两个参数,一个初始值这里是0,一个BinaryOperator accumulator 来将两个元素结合起来产生一个新值, 另外reduce方法还有一个没有初始化值的重载方法

获取流中最小最大值

  1. OptionalInt min = menu.stream().mapToInt(Dish::getCalories).min();
  2. OptionalInt max = menu.stream().mapToInt(Dish::getCalories).max();

通过summarizingInt同时求总和、平均值、最大值、最小值

  1. IntSummaryStatistics intSummaryStatistics = menu.stream().collect(summarizingInt(Dish::getCalories));
  2. double average = intSummaryStatistics.getAverage(); //获取平均值
  3. int min = intSummaryStatistics.getMin(); //获取最小值
  4. int max = intSummaryStatistics.getMax(); //获取最大值
  5. long sum = intSummaryStatistics.getSum(); //获取总和

返回集合

  1. List<String> strings = menu.stream().map(Dish::getName).collect(toList());
  2. Set<String> sets = menu.stream().map(Dish::getName).collect(toSet());

通过joining拼接流中的元素

  1. String result = menu.stream().map(Dish::getName).collect(Collectors.joining(", "));

进阶通过groupingBy进行分组

  1. Map<Type, List<Dish>> result = dishList.stream().collect(groupingBy(Dish::getType));

在collect方法中传入groupingBy进行分组,其中groupingBy的方法参数为分类函数。还可以通过嵌套使用groupingBy进行多级分类

  1. Map<Type, List<Dish>> result = menu.stream().collect(groupingBy(Dish::getType,
  2. groupingBy(dish -> {
  3. if (dish.getCalories() <= 400) return CaloricLevel.DIET;
  4. else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
  5. else return CaloricLevel.FAT;
  6. })));