简体   繁体   中英

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, eg by initializing and populating the map in one statement?

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM