日期:2014-05-17  浏览次数:20768 次

List<Map>、、、合并Map问题
如果我有如下格式的数据:
Java code
        List<Map> list = new ArrayList<Map>();
        Map map1 = new HashMap();
        map1.put("submitDate", "2011-12-10");
        map1.put("scoreValue", 20);
        Map map2 = new HashMap();
        map2.put("submitDate", "2011-12-10");
        map2.put("scoreValue", 10);
        Map map3 = new HashMap();
        map3.put("submitDate", "2011-12-11");
        map3.put("scoreValue", 11);
        Map map4 = new HashMap();
        map4.put("submitDate", "2011-12-11");
        map4.put("scoreValue", 11);
        Map map5 = new HashMap();
        map5.put("submitDate", "2011-12-12");
        map5.put("scoreValue", 5);
list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);

如何高效率的把submitDate对应的value相同的map合并、而且把对应的scoreValue的值累加起来、类似
 map1.put("submitDate", "2011-12-10"); map2.put("submitDate", "2011-12-11");
 map1.put("scoreValue", 30); map2.put("scoreValue", 22);
。。。。。

------解决方案--------------------
Java code

Map<String,List> map = new HashMap<String,List>();
if(map.get(key) == null)
{
    map.put(key , new ArrayList());
}
map.get(key).add(value);

------解决方案--------------------
试试看是不是你想要的结果:
Java code

List<Map> list = new ArrayList<Map>();
        Map map1 = new HashMap();
        map1.put("submitDate", "2011-12-10");
        map1.put("scoreValue", 20);
        Map map2 = new HashMap();
        map2.put("submitDate", "2011-12-10");
        map2.put("scoreValue", 10);
        Map map3 = new HashMap();
        map3.put("submitDate", "2011-12-11");
        map3.put("scoreValue", 11);
        Map map4 = new HashMap();
        map4.put("submitDate", "2011-12-11");
        map4.put("scoreValue", 11);
        Map map5 = new HashMap();
        map5.put("submitDate", "2011-12-12");
        map5.put("scoreValue", 5);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        
        List<Map> mapList = new ArrayList<Map>();
        for (int i = 0; i < list.size(); i++) {
            Map m1 = list.get(i);
            mapList.add(list.get(i));
            for (int j = 0; j < mapList.size(); j++) {
                if(i<list.size()-1){
                    Map m2 = list.get(i+1);
                    Map m3 = mapList.get(j);
                    if(m2.get("submitDate").equals(m3.get("submitDate"))){
                        m2.put("scoreValue", (Integer)m3.get("scoreValue")+(Integer)m2.get("scoreValue"));
                        mapList.remove(m1);
                    }
                }
            }
        }
        
        System.out.println(mapList);

------解决方案--------------------
Java code

Map map = new HashMap();
for(int i = 0; i < list.size(); i++){
  if(map.get(list.get(i).get("submitDate")) == null){
       map.put(list.get(i).get("submitDate"), list.get(i).get("scoreValue"));
  }else{
       map.put(list.get(i).get("submitDate"), 
           map.get(list.get(i).get("submitDate")) + list.get(i).get("scoreValue"));
  }
}

------解决方案--------------