不同字段的Map集合合并

  1 import com.alibaba.fastjson.JSONArray;
  2 import com.alibaba.fastjson.JSONObject;
  3 
  4 import java.util.ArrayList;
  5 import java.util.HashMap;
  6 import java.util.List;
  7 import java.util.Map;
  8 import java.util.stream.Collectors;
  9 
 10 public class Test {
 11 
 12     public static void main(String[] args) {
 13 
 14         List<Map<String,Object>> maps = new ArrayList<>();
 15         Map<String,Object> map1 = new HashMap<>();
 16         map1.put("name", "北京");
 17         map1.put("date", "2023-01-01");
 18         map1.put("value", "11");
 19         maps.add(map1);
 20         Map<String,Object> map2 = new HashMap<>();
 21         map2.put("name", "天津");
 22         map2.put("date", "2023-01-01");
 23         map2.put("value", "12");
 24         maps.add(map2);
 25         Map<String,Object> map3 = new HashMap<>();
 26         map3.put("name", "上海");
 27         map3.put("date", "2023-01-01");
 28         map3.put("value", "13");
 29         maps.add(map3);
 30         Map<String,Object> map4 = new HashMap<>();
 31         map4.put("name", "深圳");
 32         map4.put("date", "2023-01-01");
 33         map4.put("value", "14");
 34         maps.add(map4);
 35 
 36         Map<String,Object> map11 = new HashMap<>();
 37         map11.put("name", "北京");
 38         map11.put("date", "2023-01-02");
 39         map11.put("value", "101");
 40         maps.add(map11);
 41         Map<String,Object> map22 = new HashMap<>();
 42         map22.put("name", "天津");
 43         map22.put("date", "2023-01-02");
 44         map22.put("value", "102");
 45         maps.add(map22);
 46         Map<String,Object> map33 = new HashMap<>();
 47         map33.put("name", "上海");
 48         map33.put("date", "2023-01-02");
 49         map33.put("value", "103");
 50         maps.add(map33);
 51         Map<String,Object> map44 = new HashMap<>();
 52         map44.put("name", "深圳");
 53         map44.put("date", "2023-01-02");
 54         map44.put("value", "104");
 55         maps.add(map44);
 56 
 57         System.out.println(JSONObject.toJSONString(maps));
 58 
 59         Map<String, List<Object>> result = maps.stream()
 60                 .flatMap(i -> i.entrySet().stream())
 61                 .collect(Collectors.groupingBy(Map.Entry::getKey,
 62                         Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
 63 
 64         List<Object> dates = result.get("date");
 65         List<Object> names = result.get("name");
 66         List<Object> values = result.get("value");
 67 
 68         //天数
 69         List<Object> days = dates.stream().distinct().collect(Collectors.toList());
 70 
 71         //组织机构个数
 72         int orgCount = dates.size() / days.size();
 73 
 74         System.out.println("--------------------");
 75 
 76         List<Map<String,Object>> resultList = new ArrayList<>();
 77 
 78         for (int j = 0; j < days.size(); j++) {
 79             String day = days.get(j).toString();
 80 
 81             int start = j * orgCount;
 82             int size = orgCount;
 83 
 84             List<Object> _names = names.subList(start, start+size);
 85             List<Object> _values = values.subList(start, start+size);
 86 
 87             for (int i = 0; i < orgCount; i++) {
 88 
 89                 Map<String,Object> map_ = new HashMap<>();
 90                 Object name = _names.get(i);
 91                 Object value = _values.get(i);
 92 
 93                 if (resultList.stream().anyMatch(k -> k.get("NAME").equals(name))) {
 94                     resultList.get(i).put(day,value);
 95                 }else{
 96                     map_.put("NAME", name);
 97                     map_.put(day, value);
 98                     resultList.add(map_);
 99                 }
100             }
101         }
102         System.out.println(JSONArray.toJSONString(resultList));
103     }
104     
105 }

57行结果:

[
    {
        "date":"2023-01-01",
        "name":"北京",
        "value":"11"
    },
    {
        "date":"2023-01-01",
        "name":"天津",
        "value":"12"
    },
    {
        "date":"2023-01-01",
        "name":"上海",
        "value":"13"
    },
    {
        "date":"2023-01-01",
        "name":"深圳",
        "value":"14"
    },
    {
        "date":"2023-01-02",
        "name":"北京",
        "value":"101"
    },
    {
        "date":"2023-01-02",
        "name":"天津",
        "value":"102"
    },
    {
        "date":"2023-01-02",
        "name":"上海",
        "value":"103"
    },
    {
        "date":"2023-01-02",
        "name":"深圳",
        "value":"104"
    }
]

102行最终结果:

[
    {
        "2023-01-01":"11",
        "2023-01-02":"101",
        "NAME":"北京"
    },
    {
        "2023-01-01":"12",
        "2023-01-02":"102",
        "NAME":"天津"
    },
    {
        "2023-01-01":"13",
        "2023-01-02":"103",
        "NAME":"上海"
    },
    {
        "2023-01-01":"14",
        "2023-01-02":"104",
        "NAME":"深圳"
    }
]

 

posted @ 2023-03-16 20:10  大瘦猴  阅读(45)  评论(0编辑  收藏  举报