我有以下收藏:Set<Map.Entry<Event, Long>> entries歌唱活動:public class Event{ private long epoch; private List<Pair<String, String> eventParams; }我想將條目集合轉(zhuǎn)換為Map<String, Set<String>>例子:List<Pair<String, String> eventParams = Arrays.asList(Pair.of("abc","123"), Pair.of("abc","456"));轉(zhuǎn)換后的集合: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。這樣做的正確方法是什么?
1 回答

哈士奇WWW
TA貢獻1799條經(jīng)驗 獲得超6個贊
代替
Collectors.toSet(Pair::getValue)
和
Collectors.mapping(Pair::getValue,?Collectors.toSet())
問題是它Collectors.toSet()
沒有任何參數(shù),它對輸入流定義的類型進行操作。Collectors.mapping(mapper, downstream)
通過“在累積之前將映射函數(shù)應用于每個輸入元素”來改變此行為。
添加回答
舉報
0/150
提交
取消