【发布时间】:2018-07-04 15:22:31
【问题描述】:
我有这个代码:
List<String> list = Arrays.asList("a", "b", "c", "a", "c");
Map<String, List<String>> map = list.stream().collect(Collectors.groupingBy(
Function.identity() ,
Collectors.toCollection(ArrayList::new)
));
它产生的是什么
{a=[a, a], b=[b], c=[c, c]}
请问我应该如何编写映射方法来获得可以给我的地图:
{a=2, b=1, c=2}
【问题讨论】:
-
您可以先将
Map<String, List<String>> map更改为Map<String, Integer> map- 因为您不想要String(s) 的列表。
标签: java java-stream