Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatically creating a multi-value Map from a Stream in Java 8

Is there any way to elegantly initialize and populate a multi-value Map<K,Collection<V>> using Java 8's stream API?

I know it's possible to create a single-value Map<K, V> using the Collectors.toMap(..) functionalities:

Stream<Person> persons = fetchPersons();
Map<String, Person> personsByName = persons.collect(Collectors.toMap(Person::getName, Function.identity()));

Unfortunately, that method won't work well for possibly non-unique keys such as a person's name.

On the other hand, it's possible to populate a multi-value Map<K, Collection<V>> using Map.compute(K, BiFunction<? super K,? super V,? extends V>>):

Stream<Person> persons = fetchPersons();
Map<String, Set<Person>> personsByName = new HashMap<>();
persons.forEach(person -> personsByName.compute(person.getName(), (name, oldValue) -> {
    Set<Person> result = (oldValue== null) ? new HashSet<>() : oldValue;
    result.add(person);
    return result;
}));

Is there no more concise way of doing this, e.g. by initializing and populating the map in one statement?

like image 502
errantlinguist Avatar asked Aug 08 '17 11:08

errantlinguist


1 Answers

If you use forEach, it’s much simpler to use computeIfAbsent instead of compute:

Map<String, Set<Person>> personsByName = new HashMap<>();
persons.forEach(person ->
    personsByName.computeIfAbsent(person.getName(), key -> new HashSet<>()).add(person));

However, when using the Stream API, it’s preferable to use collect. In this case, use groupingBy instead of toMap:

Map<String, Set<Person>> personsByName =
    persons.collect(Collectors.groupingBy(Person::getName, Collectors.toSet());
like image 65
Holger Avatar answered Sep 19 '22 12:09

Holger