有没有办法Map<K,Collection<V>>使用Java 8的流API 优雅地初始化和填充多值?
我知道可以Map<K, V>使用Collectors.toMap(..)功能创建单值:
Stream<Person> persons = fetchPersons();
Map<String, Person> personsByName = persons.collect(Collectors.toMap(Person::getName, Function.identity()));
Run Code Online (Sandbox Code Playgroud)
不幸的是,该方法不适用于可能的非唯一键,例如人名.
另一方面,可以Map<K, Collection<V>>使用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;
}));
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法来做到这一点,例如通过在一个语句中初始化和填充地图?