JDK8之Collectors之分组平均

public class CollectorsTest02 {

public static void main(String[] args) {

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

//1、计算集合中calories的平均数
Optional.ofNullable(menu.stream().collect(Collectors.averagingDouble(TestDemo::getCalories))).ifPresent(System.out::println);
Optional.ofNullable(menu.stream().collect(Collectors.averagingInt(TestDemo::getCalories))).ifPresent(System.out::println);
Optional.ofNullable(menu.stream().collect(Collectors.averagingLong(TestDemo::getCalories))).ifPresent(System.out::println);
Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(Collectors.averagingDouble(TestDemo::getCalories),a -> "平均数为:"+a))).ifPresent(System.out::println);

//2、获取集合中的count
Optional.ofNullable(menu.stream().collect(Collectors.counting())).ifPresent(System.out::println);

//3、对集合数据进行先分组后求平均的操作
Map<TestDemo.Type,Double> map = menu.stream().collect(Collectors.groupingBy(TestDemo::getType,Collectors.averagingDouble(TestDemo::getCalories)));
Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(TestDemo::getType,Collectors.averagingDouble(TestDemo::getCalories)),
a -> "分组平均结果为:"+a))).ifPresent(System.out::println);
Optional.ofNullable(map.getClass()).ifPresent(System.out::println);
Map<TestDemo.Type,Double> collect = menu.stream().collect(Collectors.groupingBy(TestDemo::getType, TreeMap::new,Collectors.averagingDouble(TestDemo::getCalories)));
Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(TestDemo::getType,TreeMap::new,
Collectors.averagingDouble(TestDemo::getCalories)),a -> "把结果转成TreeMap输出:"+a))).ifPresent(System.out::println);
Optional.ofNullable(collect.getClass()).ifPresent(System.out::println);

//4、count、求和、最小、最大、平均数
IntSummaryStatistics collect1 = menu.stream().collect(Collectors.summarizingInt(TestDemo::getCalories));
Optional.ofNullable(collect1).ifPresent(System.out::println);


}

static class TestDemo {
private String name;
private boolean vegetarian;
private int calories;
private Type type;

public TestDemo(String name, boolean vegetarian, int calories, Type type) {
this.name = name;
this.vegetarian = vegetarian;
this.calories = calories;
this.type = type;
}

public String getName() {
return name;
}

public boolean isVegetarian() {
return vegetarian;
}

public int getCalories() {
return calories;
}

public Type getType() {
return type;
}

public enum Type {MEAT, FISH, OTHER}


@Override
public String toString() {
return "TestDemo{" +
"name='" + name + '\'' +
", vegetarian=" + vegetarian +
", calories=" + calories +
", type=" + type +
'}';
}
}

}
posted @ 2022-01-06 14:51  java_my_skill  阅读(78)  评论(0)    收藏  举报