JAVA8-Collectors API: partitioningBy、reducing、summarizingLong、summarizingInt、summarizingDouble

admin
2022-09-18 / 0 评论 / 149 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年09月18日,已超过796天没有更新,若内容或图片失效,请留言反馈。

JAVA8-Collectors API: partitioningBy、reducing、summarizingLong、summarizingInt、summarizingDouble

前置数据:

    public static final List<Dish> menu = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER),
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 300, Dish.Type.FISH),
            new Dish("salmon", false, 450, Dish.Type.FISH));

范例:

1、partitioningBy 分组

    public static void testPartitioningByWithPredicate() {
        System.out.println("testPartitioningByWithPredicate");
        Map<Boolean, List<Dish>> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testPartitioningByWithPredicate
{false=[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}, Dish{name='chicken', vegetarian=false, calories=400, type=MEAT}, Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}], true=[Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}]}

2、partitioningBy 分组后,再处理求平均数

    public static void testPartitioningByWithPredicateAndCollecotr() {
        System.out.println("testPartitioningByWithPredicateAndCollecotr");
        Map<Boolean, Double> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.averagingInt(Dish::getCalories)));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testPartitioningByWithPredicateAndCollecotr
{false=530.0, true=387.5}

3、reducing

    public static void testReducingBinaryOperator() {
        System.out.println("testReducingBinaryOperator");
        Optional<Dish> collect = menu.stream().collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparingInt(Dish::getCalories))));
        collect.ifPresent(System.out::println);
    }

输出结果:

testReducingBinaryOperator
Dish{name='pork', vegetarian=false, calories=800, type=MEAT}

4、reducing

    public static void testReducingBinaryOperatorAndIdentity() {
        System.out.println("testReducingBinaryOperatorAndIdentity");
        Integer collect = menu.stream().map(Dish::getCalories).collect(Collectors.reducing(0, (d1, d2) -> d1 + d2));
        System.out.println(collect);
    }

输出结果:

testReducingBinaryOperatorAndIdentity
4200

5、reducing

    public static void testReducingBinaryOperatorAndIdentityAndFunction() {
        System.out.println("testReducingBinaryOperatorAndIdentityAndFunction");
        Integer collect = menu.stream().collect(Collectors.reducing(0, Dish::getCalories,(d1, d2) -> d1 + d2));
        System.out.println(collect);
    }

输出结果:

testReducingBinaryOperatorAndIdentityAndFunction
4200

6、summarizingDouble

     public static void testSummarizingDouble() {
        System.out.println("testSummarizingDouble");
        DoubleSummaryStatistics collect = menu.stream().collect(Collectors.summarizingDouble(Dish::getCalories));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testSummarizingDouble
DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}

7、summarizingInt

    public static void testSummarizingInt() {
        System.out.println("testSummarizingDouble");
        IntSummaryStatistics collect = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testSummarizingDouble
IntSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}

8、summarizingLong

    public static void testSummarizingLong() {
        System.out.println("testSummarizingLong");
        LongSummaryStatistics collect = menu.stream().collect(Collectors.summarizingLong(Dish::getCalories));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testSummarizingLong
LongSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}
2

评论 (0)

取消