JAVA8-Collectors API: groupingByConcurrent、joining、mapping、maxby、minby

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

JAVA8-Collectors API: groupingByConcurrent、joining、mapping、maxby、minby

前置数据

    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、groupingByConcurrent 返回map为ConcurrentMap,按类型分组

    public static void testGroupingByConcurrentWithFunction() {
        System.out.println("testGroupingByConcurrentWithFunction");
        ConcurrentMap<Dish.Type, List<Dish>> collect = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType));
        Optional.ofNullable(collect.getClass()).ifPresent(System.out::println);
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testGroupingByConcurrentWithFunction
class java.util.concurrent.ConcurrentHashMap
{MEAT=[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}], FISH=[Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}], OTHER=[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、groupingByConcurrent 返回map为ConcurrentMap,按按类型分组,并求平均值

    public static void testGroupingByConcurrentWithFunctionAndCollector() {
        System.out.println("testGroupingByConcurrentWithFunctionAndCollector");
        Map<Dish.Type, Double> collect = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType, Collectors.averagingInt(Dish::getCalories)));
        Optional.ofNullable(collect.getClass()).ifPresent(System.out::println);
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testGroupingByConcurrentWithFunctionAndCollector
class java.util.concurrent.ConcurrentHashMap
{MEAT=633.3333333333334, FISH=375.0, OTHER=387.5}

3、groupingByConcurrent 返回map为ConcurrentMap,按按类型分组,并求平均值,并修改返回类型为ConcurrentSkipListMap(跳表,以空间换时间的数据结构)

    public static void testGroupingByConcurrentWithFunctionAndSupplierAndCollector() {
        System.out.println("testGroupingByConcurrentWithFunctionAndSupplierAndCollector");
        ConcurrentSkipListMap<Dish.Type, Double> collect = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingInt(Dish::getCalories)));
        Optional.ofNullable(collect.getClass()).ifPresent(System.out::println);
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testGroupingByConcurrentWithFunctionAndSupplierAndCollector
class java.util.concurrent.ConcurrentSkipListMap
{MEAT=633.3333333333334, FISH=375.0, OTHER=387.5}

4、joining 拼接,不能直接join

    public static void testJoin() {
        System.out.println("testJoin");
        String collect = menu.stream().map(Dish::getName).collect(Collectors.joining());
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testJoin
porkbeefchickenfrench friesriceseason fruitpizzaprawnssalmon

5、joining 拼接,通过逗号拼接

    public static void testJoinWithDelimiter() {
        System.out.println("testJoinWithDelimter");
        String collect = menu.stream().map(Dish::getName).collect(Collectors.joining(","));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testJoinWithDelimter
pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon

6、joining 拼接,通过逗号拼接,拼接后加前缀和后缀

    public static void testJoinWithDelimiterAndPrefixAndSuffix() {
        System.out.println("testJoinWithDelimiterAndPrefixAndSuffix");
        String collect = menu.stream().map(Dish::getName).collect(Collectors.joining(",","Names:[","]"));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testJoinWithDelimiterAndPrefixAndSuffix
Names:[pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon]

7、mapping 拼接,能直接join

    public static void testMapping() {
        System.out.println("testMapping");
        String collect = menu.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(",")));
        Optional.ofNullable(collect).ifPresent(System.out::println);
    }

输出结果:

testMapping
pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon

8、maxBy 查找最大的一个Dish

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

输出结果:

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

9、minBy 查找最小的一个Dish

    public static void testMinBy() {
        System.out.println("testMinBy");
        Optional<Dish> collect = menu.stream().collect(Collectors.minBy(Comparator.comparing(Dish::getCalories)));
        collect.ifPresent(System.out::println);
    }

输出结果:

testMinBy
Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}
3

评论 (0)

取消