java8 Stream list to Map key 重复 value合并到Collectio的操作
作者:不泪之城 时间:2022-02-01 23:53:58
java8 Stream list to Map key 重复 value合并到Collectio
关于把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合并重复数据(map中key相等的value值相加)
public static void main(String[] args) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", 1);
map1.put("value", 2);
list.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", 1);
map2.put("value", 5);
list.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", 2);
map3.put("value", 5);
list.add(map3);
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("id", 2);
map4.put("value", 4);
list.add(map4);
Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("id", 1);
map5.put("value", 4);
list.add(map5);
//id相等value相加(合并id相同数据)
System.out.println("原始数据:"+list);
//方法一
Map<String, Object> result1 = new HashMap<String, Object>();
for(Map<String, Object> map : list){
String id = map.get("id").toString();
Long value = Long.parseLong(map.get("value").toString());
if(result1.containsKey(id)){
Long temp = Long.parseLong(result1.get(id).toString());
value += temp;
}
result1.put(id, value);
}
System.out.println("合并后的数据:"+result1);
//方法二
Map<String, Map<String, Object>> result2 = new HashMap<String, Map<String,Object>>();
for(Map<String, Object> map : list){
String id = map.get("id").toString();
Long value = Long.parseLong(map.get("value").toString());
if(result2.containsKey(id)){
Long temp = Long.parseLong(result2.get(id).get("value").toString());
value += temp;
result2.get(id).put("value", value);
continue;
}
result2.put(id, map);
}
System.out.println("合并后的数据2:"+result2);
}
来源:https://blog.csdn.net/abc123maxiaolong/article/details/84906381
标签:java8,key,重复,value,Collectio
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis Generator 自定义生成注释的方法
2023-02-24 12:37:23
![](https://img.aspxhome.com/file/2023/1/64031_0s.png)
JavaWeb简单文件上传流程的实战记录
2023-04-02 09:14:59
![](https://img.aspxhome.com/file/2023/9/71889_0s.png)
java简单实现复制 粘贴 剪切功能代码分享
2023-10-14 05:08:23
MyBatis-Plus 查询返回实体对象还是map
2023-11-28 03:20:19
JavaWeb登录界面登录失败在同一页面进行提示的解决
2023-11-26 15:51:34
![](https://img.aspxhome.com/file/2023/6/60356_0s.png)
Struts2实现对action请求对象的拦截操作方法
2023-06-08 01:54:13
![](https://img.aspxhome.com/file/2023/2/70342_0s.jpg)
Spring Boot使用Allatori代码混淆的方法
2023-11-24 16:34:55
![](https://img.aspxhome.com/file/2023/0/59080_0s.jpg)
详解Java设计模式之抽象工厂模式
2022-09-29 17:00:34
应用启动数据初始化接口CommandLineRunner和Application详解
2023-02-06 05:00:33
![](https://img.aspxhome.com/file/2023/8/65608_0s.jpg)
深入浅析C# 11 对 ref 和 struct 的改进
2021-09-27 20:31:47
C#词法分析器之正则表达式的使用
2023-06-21 13:10:58
![](https://img.aspxhome.com/file/2023/2/66592_0s.png)
JavaWeb 中Cookie实现记住密码的功能示例
2023-04-06 05:06:48
![](https://img.aspxhome.com/file/2023/2/69102_0s.png)
Java单例模式的8种写法(推荐)
2023-01-06 14:23:27
![](https://img.aspxhome.com/file/2023/4/76614_0s.png)
Java Synchronized锁失败案例及解决方案
2023-10-25 12:33:47
![](https://img.aspxhome.com/file/2023/3/61863_0s.png)
Javassist如何操作Java 字节码
2021-08-09 08:21:28
![](https://img.aspxhome.com/file/2023/2/76582_0s.jpg)
Java 多线程并发编程提高数据处理效率的详细过程
2021-06-29 04:19:39
![](https://img.aspxhome.com/file/2023/6/65766_0s.jpg)
JAVA面试题 简谈你对synchronized关键字的理解
2022-09-17 17:06:05
详解Spring ApplicationContext加载过程
2023-07-17 01:07:43
推荐史上最全的IDEA好用插件
2023-02-22 21:35:04
![](https://img.aspxhome.com/file/2023/8/62378_0s.png)
Spring利用@Validated注解实现参数校验详解
2023-08-26 12:06:26