Collectors.toConcurrentMap和通过Collectors.toMap供应商选项将Map转换为ConcurrentHashMap有什么区别? [英] What is the difference between Collectors.toConcurrentMap and converting a Map to ConcurrentHashMap via Collectors.toMap supplier option?

查看:5586
本文介绍了Collectors.toConcurrentMap和通过Collectors.toMap供应商选项将Map转换为ConcurrentHashMap有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Java 8 Stream将 Map 转换为 ConcurrentHashMap code>和收藏家界面,我可以使用两个选项。

I want to convert a Map into a ConcurrentHashMap via Java 8 Stream and Collector interface, and there are two options I can use.

第一个:

Map<Integer, String> mb = persons.stream()
                                 .collect(Collectors.toMap(
                                            p -> p.age, 
                                            p -> p.name, 
                                            (name1, name2) -> name1+";"+name2,
                                            ConcurrentHashMap::new));

第二个:

Map<Integer, String> mb1 = persons.stream()
                                  .collect(Collectors.toConcurrentMap(
                                             p -> p.age, 
                                             p -> p.name));

哪一个更好?我何时应该使用每个选项?

Which one is the better option? When should I use each option?

推荐答案

处理并行流时它们之间存在差异。

There is a difference between them when dealing with parallel streams.

toMap - >是非并发收集器

toMap -> is a non-concurrent collector

toConcurrentMap - >是一个并发收集器(这可以从他们的特征中看出)。

toConcurrentMap -> is a concurrent collector (this can be seen from their characteristics).

区别在于 toMap 将创建多个中间结果,然后将合并在一起(此类收集器的供应商将被多次调用),而 toConcurrentMap 将创建单个结果,每个线程都会向其投掷结果(此类收集器的供应商只会被调用一次)

The difference is that toMap will create multiple intermediate results and then will merge then together (the Supplier of such a Collector will be called multiple times), while toConcurrentMap will create a single result and each Thread will throw results at it (the Supplier of such a Collector will be called only once)

为什么这很重要?这涉及插入顺序(如果重要)。

Why is this important? This deals with insertion order (if that matters).

toMap 将通过合并多个中间结果在遭遇顺序中在结果Map中插入值(多次调用该收集器的供应商以及组合器)

toMap will insert values in the resulting Map in encounter order by merging multiple intermediate results (Supplier of that collector is called multiple time as well as the Combiner)

toConcurrentMap 将通过抛出所有元素以任何顺序(未定义)收集元素在公共结果容器(在这种情况下为ConcurrentHashMap)。供应商只召集一次,Accumulator多次召唤,而Combiner永远不召唤。

toConcurrentMap will collect elements in any order (undefined) by throwing all elements at a common result container (ConcurrentHashMap in this case). Supplier is called only once, Accumulator many times and Combiner never.

这里的一个小警告是,对于 CONCURRENT 收集器,不要调用合并:要么流必须要有 UNORDERED 标志 - 通过无序()显式调用或未订购流源(a 例如设置

The small caveat here is that for a CONCURRENT collector to not invoke the merger: either the stream has to have the UNORDERED flag - either via the unordered() explicit call or when the source of the stream is not ordered (a Set for example).

这篇关于Collectors.toConcurrentMap和通过Collectors.toMap供应商选项将Map转换为ConcurrentHashMap有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆