java8之CollectorsAPI详解(带实例)02

写给自己避免忘掉:

接着更这官网API 的节奏介绍我们的Collectors的API:

groupingByConcurrent

这个意思跟前面的分组功能一样,只不过把返回的类型变成了ConcurrentMap,之前是Map类型的

接着上上一篇的写我们的实体类还是那样,数据还是那点需求:根据type类型来对里面的元素进行分组:

private 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);
}

当然里面还可以有两个参数:(根据lei类型分组之后并算出他们卡路里的平均值)

private static void testGroupingByConcurrentWithFunctionAndCollector() {
    System.out.println("testGroupingByConcurrentWithFunctionAndCollector");
    ConcurrentMap<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);
}

同样的我们还有三个参数的,分组之后算出平均值,把返回类型有ConcurrentMap变为SpikListMap:

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

这里我们说一下SkipListMap 他是典型的拿空间换时间,也就是跳表的数据结构,跟红黑树比的话,效率高于红黑树

我们执行一下上面的方法,并输出结果:

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

我们看到,最后一个类型已近被转化

剩下的API:


Join的三个方法:

private static void testJoining() {
    System.out.println("testJoining");
    Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining()))
            .ifPresent(System.out::println);
}


private static void testJoiningWithDelimiter() {
    System.out.println("testJoiningWithDelimiter");
    Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining(",")))
            .ifPresent(System.out::println);
}

private static void testJoiningWithDelimiterAndPrefixAndSuffix() {
    System.out.println("testJoiningWithDelimiterAndPrefixAndSuffix");
    Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining(",", "Names[", "]")))
            .ifPresent(System.out::println);
}

我们看一下输出就知道什么意思:

testJoining
porkbeefchickenfrench friesriceseason fruitpizzaprawnssalmon
testJoiningWithDelimiter
pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon
testJoiningWithDelimiterAndPrefixAndSuffix

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

我们这里先把名字拼接出来然后又拼接个",",最后头和尾都有东西:

接着

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

private static void testMaxBy() {
    System.out.println("testMaxBy");
    Optional.ofNullable(menu.stream().collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories)))).ifPresent(System.out::println);

}

private static void testMinBy() {
    System.out.println("testMinBy");
    menu.stream().collect(Collectors.minBy(Comparator.comparingInt(Dish::getCalories)))
            .ifPresent(System.out::println);
}
testMapping
pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon
testMaxBy
Optional[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}]
testMinBy

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

我们看到mapping效果个和join一样,但是少了用map来对他进行取值,直接就能取



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值