java合并两个List <map <string,object =“” >>到HashMap <string,object =“”>

时间:2018-06-30 14:28:02

标签: java list

我有两个名为List<Map<String, Object>> cpuReportList<Map<String, Object>> memoryReport的列表,我想将这两个列表放入一个HashMap中,其中cpureportmemoryReport在HashMap中可以具有相同或不同的键

memoryReport : [{robot=GEMPSAPPPROD02, memory_min=3.17, memory_max=90.79, memory_avg=31.857100643332412, memory_50=34006, memory_60=5700, memory_65=2480, memory_100=2450}, {robot=MUMGEMIDADBPROD05, memory_min=6.07, memory_max=8.75, memory_avg=8.184755153233644, memory_50=44635, memory_60=0, memory_65=0, memory_100=0}]
cpuReport : [{robot=Hema, cpu_min=1.12, cpu_max=1.12, cpu_avg=1.12, cpu_50=0, cpu_60=0, cpu_65=0, cpu_100=0}, GEMPSAPPPROD02={robot=GEMPSAPPPROD02, cpu_min=0.03, cpu_max=35.88, cpu_avg=2.6957142857143097, cpu_50=44636, cpu_60=0, cpu_65=0, cpu_100=0}]
mergedMap: GEMPSAPPPROD02={robot=GEMPSAPPPROD02, memory_min=3.17, memory_max=90.79, memory_avg=31.857100643332412, memory_50=34006, memory_60=5700, memory_65=2480, memory_100=2450,cpu_min=0.03, cpu_max=35.88, cpu_avg=2.6957142857143097, cpu_50=44636, cpu_60=0, cpu_65=0, cpu_100=0},MUMGEMIDADBPROD05={robot=MUMGEMIDADBPROD05, memory_min=6.07, memory_max=8.75, memory_avg=8.184755153233644, memory_50=44635, memory_60=0, memory_65=0, memory_100=0},Hema={robot=Hema, cpu_min=1.12, cpu_max=1.12, cpu_avg=1.12, cpu_50=0, cpu_60=0, cpu_65=0, cpu_100=0}

public List<Map<String, Object>> getMergedReport(List<Map<String, Object>> cpuReport,
        List<Map<String, Object>> memoryReport) {
    Map<String, Object> MergedMap = new HashMap<String, Object>();
    List<Map<String, Object>> listOfBothMaps = new ArrayList<>();
    String robotFromcpu = null;
    try {
        for (Map<String, Object> cpuMap : cpuReport) {
            robotFromcpu = (String) cpuMap.get("robot");
            MergedMap.put(robotFromcpu, cpuMap);
            }
        for (Map<String, Object> memoryMap : memoryReport) {
            String robotFromMemory = (String) memoryMap.get("robot");
            Map<String, Object> map = (Map<String, Object>) MergedMap.get(robotFromMemory);
            if(map != null){
                map.putAll(memoryMap);
            }
            else{
                MergedMap.put(robotFromMemory, memoryMap);
            }
            LOGGER.info("The merged map is : " + map);
            listOfBothMaps.add(map);
        }
        } catch (Exception e) {
        LOGGER.info("The exception is : " + e.getMessage());
    }
    return listOfBothMaps;
}

工作代码:

 Map<String, Object> mergedMap = new HashMap<String, Object>();
        List<Map<String, Object>> listOfMergedMaps = new ArrayList<>();
        String robotFromcpu = null;
            for (Map<String, Object> cpuMap : cpuReport) {
                robotFromcpu = (String) cpuMap.get("robot");
                mergedMap.put(robotFromcpu, cpuMap);
                }
            for (Map<String, Object> memoryMap : memoryReport) {
                String robotFromMemory = (String) memoryMap.get("robot");
                Map<String, Object> map = (Map<String, Object>) mergedMap.get(robotFromMemory);
                if(map != null){
                    map.putAll(memoryMap);
                    LOGGER.info("The merged map is : " + map);
                    }else{
                    mergedMap.put(robotFromMemory, memoryMap);
                }
            }
            for(Map.Entry<String, Object> map : mergedMap.entrySet()) {
                listOfMergedMaps.add((Map<String, Object>)map.getValue());
            }

1 个答案:

答案 0 :(得分:0)

下面应该可以工作:

public List<Map<String, Object>> getMergedReport(List<Map<String, Object>> cpuReport,
        List<Map<String, Object>> memoryReport) {
    Map<String, Object> MergedMap = new HashMap<String, Object>();
    List<Map<String, Object>> listOfBothMaps = new ArrayList<>();
    cpuReport.addAll(memoryReport); // merge two lists
    Iterator itr_mergedList= cpuReport.iterator(); // get iterator for list
    while(itr_mergedList.hasNext())
    {
        HashMap m= (HashMap)itr_mergedList.next();
        Set s = m.entrySet(); // get no. of entries in above list 
        Iterator itr_entrySet= s.iterator(); // get iterator for above entry set
        while(itr_entrySet.hasNext())
        {
            Map.Entry en=(Map.Entry) itr_entrySet.next();  
            MergedMap.put((String)en.getKey(), en.getValue()); // put in merged map
        }
    }
    listOfBothMaps.add(MergedMap); // add map obj to list 
    return listOfBothMaps;
}