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来对他进行取值,直接就能取



阅读更多
个人分类: java8 Collectors
想对作者说点什么? 我来说一句

Java8实战中文版

2017年11月13日 12.87MB 下载

java8实战--文字,带目录

2018年04月08日 12.87MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭