合并map中key相同的value

这几天工作中遇到的问题,后台返回的是一个List<Map<Object,Object>>数组,其中每个map中只有一组值,但是这些map中有key相同的,需要将key相同的value合并成一个list

具体要求如下

List<Map> list = new ArrayList<>();
        Map map0 = new HashMap();
        map0.put(1, 1);
        list.add(map0);
        Map map1 = new HashMap();
        map1.put(3, 4);
        list.add(map1);
        Map map2 = new HashMap();
        map2.put(1, 2);
        list.add(map2);
        Map map3 = new HashMap();
        map3.put(1, 3);
        list.add(map3);
        Map map4 = new HashMap();
        map4.put(2, 2);
        list.add(map4);
        Map map5 = new HashMap();
        map5.put(2, 1);
        list.add(map5);
        Map map6 = new HashMap();
        map6.put(3, 1);
        list.add(map6);

        // 要求将上面的List<Map>中的map中key相同的value合并
        // 最终得到下面的结果Map<Object,List>,其中几个map分别为
        // 1->[1,2,3]
        // 2->[2,1]
        // 3->[4,3]

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class CombineVale {
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. List<Map> list = new ArrayList<>();
  10. Map map0 = new HashMap();
  11. map0.put( 1, 1);
  12. list.add(map0);
  13. Map map1 = new HashMap();
  14. map1.put( 3, 4);
  15. list.add(map1);
  16. Map map2 = new HashMap();
  17. map2.put( 1, 2);
  18. list.add(map2);
  19. Map map3 = new HashMap();
  20. map3.put( 1, 3);
  21. list.add(map3);
  22. Map map4 = new HashMap();
  23. map4.put( 2, 2);
  24. list.add(map4);
  25. Map map5 = new HashMap();
  26. map5.put( 2, 1);
  27. list.add(map5);
  28. Map map6 = new HashMap();
  29. map6.put( 3, 1);
  30. list.add(map6);
  31. // 要求将上面的List<Map>中的map中key相同的value合并
  32. // 最终得到下面的结果List<Map<Object,List>>,其中三个map分别为
  33. // 1->[1,2,3]
  34. // 2->[2,1]
  35. // 3->[4,3]
  36. Map m = mapCombine(list);
  37. System.out.println(m);
  38. }
  39. public static Map mapCombine(List<Map> list) {
  40. Map<Object, List> map = new HashMap<>();
  41. for (Map m : list) {
  42. Iterator<Object> it = m.keySet().iterator();
  43. while (it.hasNext()) {
  44. Object key = it.next();
  45. if (!map.containsKey(key)) {
  46. List newList = new ArrayList<>();
  47. newList.add(m.get(key));
  48. map.put(key, newList);
  49. } else {
  50. map.get(key).add(m.get(key));
  51. }
  52. }
  53. }
  54. return map;
  55. }
  56. }

输出结果如下{1=[1, 2, 3], 2=[2, 1], 3=[4, 1]}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Listmap合并mapkey相同value可以通过以下步骤实现: 1. 创建一个空的HashMap对象,用于存储合并后的结果。 2. 遍历List的每个map对象。 3. 对于每个map对象,遍历其的每个键值对。 4. 判断当前键值对的键是否已存在于HashMap。 - 如果键不存在于HashMap,则将该键值对直接添加到HashMap,作为新的键值对。 - 如果键已存在于HashMap,则将该键对应的值与当前值相加,并更新HashMap该键的值。 5. 完成遍历后,HashMap存储的就是合并后的结果。 下面是一个示例代码: ``` import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MapMerge { public static void main(String[] args) { List<Map<String, Integer>> mapList = new ArrayList<>(); // 假设有三个map Map<String, Integer> map1 = new HashMap<>(); map1.put("key1", 10); map1.put("key2", 20); Map<String, Integer> map2 = new HashMap<>(); map2.put("key1", 30); map2.put("key3", 40); Map<String, Integer> map3 = new HashMap<>(); map3.put("key2", 50); map3.put("key3", 60); mapList.add(map1); mapList.add(map2); mapList.add(map3); Map<String, Integer> result = new HashMap<>(); // 用于存储合并后的结果 for (Map<String, Integer> map : mapList) { for (Map.Entry<String, Integer> entry : map.entrySet()) { String key = entry.getKey(); int value = entry.getValue(); if (result.containsKey(key)) { result.put(key, result.get(key) + value); } else { result.put(key, value); } } } System.out.println(result); } } ``` 运行结果为:{key1=40, key2=70, key3=100} ### 回答2: 要合并mapkey相同value,可以按照以下步骤进行操作: 1. 遍历map的所有keyvalue。 2. 判断当前的key是否已经存在于list。 3. 如果key已经存在于list,则取出对应的value,并将当前的value加上已有的value,再存入list,替换原有的value。 4. 如果key不存在于list,则将当前的keyvalue作为一个新的键值对存入list。 5. 遍历完整个map之后,list存储的就是合并后的mapkey相同value。 下面是一个示例代码: ``` import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MergeMap { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); map.put("A", 4); map.put("D", 5); List<Map.Entry<String, Integer>> list = new ArrayList<>(); for (Map.Entry<String, Integer> entry: map.entrySet()) { boolean isExist = false; for (Map.Entry<String, Integer> existingEntry: list) { if (existingEntry.getKey().equals(entry.getKey())) { existingEntry.setValue(existingEntry.getValue() + entry.getValue()); isExist = true; break; } } if (!isExist) { list.add(entry); } } for (Map.Entry<String, Integer> entry: list) { System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); } } } ``` 运行以上代码,输出结果为: ``` Key: A, Value: 5 Key: B, Value: 2 Key: C, Value: 3 Key: D, Value: 5 ``` 可见,原先map"Key: A"的value为1和4,经过合并后变为5。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值