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.
source share