如果我有如下格式的数据:
- 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")); }}
------解决方案--------------------
- Java code
public class Test { public static void main(String[] args) { 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> list0 = new ArrayList<Map>(); boolean flag = false; for(Map map : list){ for(Map map0 : list0){ if(map.get("submitDate").equals(map0.get("submitDate"))){ int scoreValue = Integer.parseInt(map0.get("scoreValue").toString()); map0.remove("scoreValue"); map0.put("scoreValue", scoreValue+Integer.parseInt(map.get("scoreValue").toString())); flag = true; break; } } if(flag == false){ list0.add(map); } flag = false; } for(Map map : list0){ System.out.println(map.get("submitDate") + " " + map.get("scoreValue")); } }}
------解决方案--------------------
- Java code
Map<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < list.size(); i++) { String date = (String)((Map)list.get(i)).get("submitDate"); int score = (Integer)((Map)list.get(i)).get("scoreValue"); if (map.isEmpty() || !map.containsKey(date)) { map.put(date, score); } else { map.put(date, score + map.get(date)); } } List arrayList = new ArrayList(); Set<String> hashSet = map.keySet(); Iterator<String> iterator = hashSet.iterator(); while (iterator.hasNext()) { String elem = iterator.next(); Map hashMap = new HashMap(); hashMap.put("submitDate", elem); hashMap.put("scoreValue", map.get(elem)); arrayList.add(hashMap); }