相关疑难解决方法(0)

在Java 8中从流中以惯用方式创建多值Map

有没有办法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)

有没有更简洁的方法来做到这一点,例如通过在一个语句中初始化和填充地图?

java dictionary multimap java-stream

6
推荐指数
1
解决办法
3689
查看次数

标签 统计

dictionary ×1

java ×1

java-stream ×1

multimap ×1