What is the difference between Collectors.toConcurrentMap and map conversion to ConcurrentHashMap via the Collectors.toMap parameter?

I want to convert Map to ConcurrentHashMap through the Java 8 Stream and Collector interface, and there are two options that I can use.

First:

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

And the second:

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

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

+4
source share
2 answers

There is a difference between them when working with parallel threads.

toMap β†’ - non-competitive collector

toConcurrentMap β†’ is a parallel collector (this can be seen from their characteristics).

The difference is that toMap will create several intermediate results, and then merge together (the provider of such a collector will be called several times), and toConcurrentMap will create a single result and each thread will produce results on it (The provider of such a collector will be called only once)

Why is it important? This concerns the insertion order (if that matters).

toMap will insert values ​​into the resulting map in sort order, combining several intermediate results (the provider of this collector is called multiple time, as well as a combiner)

toConcurrentMap will collect elements in any order (undefined), throwing all elements into a common result container (ConcurrentHashMap in this case). The supplier is called only once, the battery many times and the Combiner never.

I would use toMap for serial stream and toConcurrentMap for parallel stream.

+5
source

From toMap Javadoc:

The returned collector is not parallel. For parallel streaming pipelines, the combiner function works by merging keys from one card to another, which can be an expensive operation. If you do not want the results to be inserted into the Map in execution order, using toConcurrentMap (Function, Function) can provide better concurrent performance.

toConcurrentMap does not insert the results into the map in order of execution, but should provide better performance.

If you do not need the insertion order, it is recommended that you use toConcurrentMap if you are using a parallel thread.

+4
source

Source: https://habr.com/ru/post/1260995/


All Articles