将 Set<V> 转换为 Map<String, Set<String>

Convert Set<V> to Map<String, Set<String>

我关注collection:

Set<Map.Entry<Event, Long>> entries

事件 POJO:

public class Event{
    private long epoch; 
    private List<Pair<String, String> eventParams; 
}

我想将条目 collection 转换为 Map<String, Set<String>>

示例:

List<Pair<String, String> eventParams = Arrays.asList(Pair.of("abc","123"), Pair.of("abc","456"));

已转换 collection:

Map<String, Set<String>> converted = ["abc", ["123", "456"]]

我试过以下方法:

entries.stream().flatMap(x -> x.getKey().getEventParams().stream())
            .collect(Collectors.groupingBy(Pair::getKey, Collectors.toSet(Pair::getValue)));

但是,我收到错误消息:无法应用收集器中的 toSet

正确的做法是什么?

替换

Collectors.toSet(Pair::getValue)

Collectors.mapping(Pair::getValue, Collectors.toSet())

问题是 Collectors.toSet() doesn't have any parameters, it operates on the type defined by the input stream. Collectors.mapping(mapper, downstream) 通过 "applying a mapping function to each input element before accumulation" 改变了这种行为。