关于把list转换成key value的map有很多博客上都有实现,这里是一个吧value放入到集合中去
List<String> list = Lists.newArrayList("1", "2", "3", "1");
Map<String, List<String>> map = list.stream().collect(Collectors.toMap(key -> key,
value -> Lists.newArrayList(value),
(List<String> newValueList, List<String> oldValueList) -> {
oldValueList.addAll(newValueList);
return oldValueList;
}));
System.out.println(JSON.toJSONString(map));
类似的,你的list泛型可以是一个bean,取bean的属性当key或者value,转换成一个集合
结果: {"1":["1","1"],"2":["2"],"3":["3"]}
List转Map实践
本文介绍了一种将List转换为带有值集合的Map的方法,并展示了如何使用Java 8 Stream API实现这一转换过程。通过实例演示了如何处理重复键并将其值收集到同一个列表中。
208

被折叠的 条评论
为什么被折叠?



