java8之CollectorsAPI详解(带实例)03

接着写API,继续:

java8之CollectorsAPI详解(带实例)03_第1张图片

废话多了也不好:

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

private static void testPartitioningByWithPredicateAndCollector() {
    System.out.println("testPartitioningByWithPredicateAndCollector");
    Map, Double> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.averagingInt(Dish::getCalories)));
    Optional.of(collect).ifPresent(System.out::println);
}

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

第一个的意思就是按照蔬菜分组如果他是Vegetarian的话就返回true,不是的 话就返回false,让后按照,结果分组

第二个,同样是vegetarian,并对求出卡路里平均

第三个,按照Calories求最大的,

以上API,只是一个例子,具体要的参数可以参考APi我的命名方式也代表参数的意思,但是没有APi解释的全

结果:

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}]}
testPartitioningByWithPredicateAndCollector
{false=530.0, true=387.5}
testReducingBinaryOperator

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

接下来是reducing:也就是聚合,Collector分为三大类,聚合 ,分组 ,分割 

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

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

当然也就是计算的,reducing这个API很常用

两个表达的意思一样,只不过参数不一样,第一个0是初始值,后面的Function是表达式,

第二个没有经过map提取,而是在Function里面进行声明,这里面也就是传两个function

testReducingBinaryOperatorAndIdentiy
4200
testReducingBinaryOperatorAndIdentiyAndFunction

4200

接着:

private static void testSummarizingDouble() {
    System.out.println("testSummarizingDouble");
    Optional.of(menu.stream().collect(Collectors.summarizingDouble(Dish::getCalories)))
            .ifPresent(System.out::println);
}

private static void testSummarizingLong() {
    System.out.println("testSummarizingLong");
    Optional.of(menu.stream().collect(Collectors.summarizingLong(Dish::getCalories)))
            .ifPresent(System.out::println);
}

private static void testSummarizingInt() {
    System.out.println("testSummarizingLong");
    Optional.of(menu.stream().collect(Collectors.summarizingInt(Dish::getCalories)))
            .ifPresent(System.out::println);
}

这几个API意思都一样只不过类型不一样,都是计算:

DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}
testSummarizingLong
LongSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}
testSummarizingLong
IntSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}

自己参悟

你可能感兴趣的:(java8,JAVA8,相关笔记)