I want to convert a Map
into a ConcurrentHashMap
via Java 8 Stream
and Collector
interface, and there are two options I can use.
The 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 one is the better option? When should I use each option?