JDK8之Collectors之聚合二

package com.etocrm.test.jdk8;


import cn.hutool.core.collection.ConcurrentHashSet;
import org.bouncycastle.util.test.Test;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

/**
* Create By peter.li
*/
public class CollectorsTest03 {

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、聚合
Optional.of(menu.stream().collect(Collectors.partitioningBy(TestDemo::isVegetarian))).ifPresent(System.out::println);
Optional.of(menu.stream().collect(Collectors.partitioningBy(TestDemo::isVegetarian,Collectors.averagingInt(TestDemo::getCalories)))).ifPresent(System.out::println);
Optional.of(menu.stream().collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(TestDemo::getCalories))))).ifPresent(System.out::println);

//2、聚合求和
Optional.of(menu.stream().map(TestDemo::getCalories).collect(Collectors.reducing(0,(a1,a2) -> a1+a2))).ifPresent(System.out::println);
Optional.of(menu.stream().collect(Collectors.reducing(0,TestDemo::getCalories,(a1,a2) -> a1+a2))).ifPresent(System.out::println);

Optional.of(menu.stream().collect(Collectors.summingDouble(TestDemo::getCalories))).ifPresent(System.out::println);
Optional.of(menu.stream().collect(Collectors.summingInt(TestDemo::getCalories))).ifPresent(System.out::println);
Optional.of(menu.stream().collect(Collectors.summingLong(TestDemo::getCalories))).ifPresent(System.out::println);
Optional.of(menu.stream().map(TestDemo::getCalories).mapToInt(Integer::intValue).sum()).ifPresent(System.out::println);

Optional.of(menu.stream().filter(t -> t.getCalories() > 600).collect(Collectors.toCollection(LinkedList::new))).ifPresent(v -> {
System.out.println(v);
System.out.println(v.getClass());
});

Optional.of(menu.stream().collect(Collectors.toConcurrentMap(TestDemo::getName,TestDemo::getCalories))).ifPresent(v -> {
System.out.println(v);
System.out.println(v.getClass());
});

Optional.of(menu.stream().collect(Collectors.toConcurrentMap(TestDemo::getType,v -> 1L,(a,b) -> a+b))).ifPresent(v -> {
System.out.println(v);
System.out.println(v.getClass());
});

/**
* 输出结果:
* {false=[TestDemo{name='pork', vegetarian=false, calories=800, type=MEAT}, TestDemo{name='beef', vegetarian=false, calories=700, type=MEAT}, TestDemo{name='chicken', vegetarian=false, calories=400, type=MEAT}, TestDemo{name='prawns', vegetarian=false, calories=300, type=FISH}, TestDemo{name='salmon', vegetarian=false, calories=450, type=FISH}], true=[TestDemo{name='french fries', vegetarian=true, calories=530, type=OTHER}, TestDemo{name='rice', vegetarian=true, calories=350, type=OTHER}, TestDemo{name='season fruit', vegetarian=true, calories=120, type=OTHER}, TestDemo{name='pizza', vegetarian=true, calories=550, type=OTHER}]}
* {false=530.0, true=387.5}
* Optional[TestDemo{name='pork', vegetarian=false, calories=800, type=MEAT}]
* 4200
* 4200
* 4200.0
* 4200
* 4200
* 4200
* [TestDemo{name='pork', vegetarian=false, calories=800, type=MEAT}, TestDemo{name='beef', vegetarian=false, calories=700, type=MEAT}]
* class java.util.LinkedList
* {season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300}
* class java.util.concurrent.ConcurrentHashMap
* {MEAT=3, OTHER=4, FISH=2}
* class java.util.concurrent.ConcurrentHashMap
*/
}

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 17:34  java_my_skill  阅读(59)  评论(0)    收藏  举报