日期:2014-05-20 浏览次数:20711 次
//原始数据源 List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); //要添加的数据源 这个新的数据源是从数据库中查询出来的。暂定执行5次也就是要向data 中添加5次 List<Map<String, Object>> newData = new ArrayList<Map<String, Object>>(); //最终的组合是这样的一个效果 List<Map<String, Object>> data key ="博士",value="12"; key ="研究生",value="13"; key ="本科",value="3"; newData ==》添加到data 中 如果key相同的话那就value相加,如果在data 中不存在相应的key 则新put一个数据到里面 比如newData key ="博士",value="3"; key ="研究生",value="3"; key ="本科",value="3"; key ="专科",value="13"; 那前三项的key都能在data中找到则value相加,key="专科"在data中找不到则添加这个map到data list 中去。
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> newData = new ArrayList<Map<String, Object>>(); for(Map newMap : newData) { Iterator<String> it = newMap.keySet().iterator(); while(it.hasNext()) { String key = it.next(); boolean find = false; for(Map map : data) { if(map.containsKey(key)) { find = true; map.put(key, String.valueOf( Integer.parse(map.get(key)) + Integer.parse(newMap.get(key)))); break; } } if(!find) { map.put(key, String.valueOf(Integer.parse(newMap.get(key)))); } } }
------解决方案--------------------
package test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class MapTest { public static void main(String []args){ Map<String,Integer> data = new HashMap<String,Integer>(); data.put("博士", 12); data.put("研究生", 13); data.put("本科", 3); Map<String,Integer> newData = new HashMap<String,Integer>(); newData.put("博士", 3); newData.put("研究生", 3); newData.put("本科", 3); newData.put("专科", 13); Iterator<String> it = newData.keySet().iterator(); while(it.hasNext()){ String s = it.next(); Integer val = data.get(s); if(val==null){ data.put(s, newData.get(s)); }else{ data.put(s, val+newData.get(s)); } } Iterator<String> t = data.keySet().iterator(); while(t.hasNext()){ String temp = t.next(); System.out.println(temp+":"+data.get(temp)); } } }
------解决方案--------------------
for example
import java.util.*; public class Test { public static void main(String []args){ List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> newData = new ArrayList<Map<String, Object>>(); //sample original data Map<String, Object> map = new HashMap<String, Object>(); map.put("博士", "12"); data.add(map); map = new HashMap<String, Object>(); map.put("研究生", "13"); data.add(map); map = new HashMap<String, Object>(); map.put("本科", 3); data.add(map); //sample new data map = new HashMap<String, Object>(); map.put("博士", "3"); newData.add(map); map = new HashMap<String, Object>(); map.put("研究生", "3"); newData.add(map); map = new HashMap<String, Object>(); map.put("本科", "3"); newData.add(map); map = new HashMap<String, Object>(); map.put("专科", "13"); newData.add(map); //merge List<Map<String, Object>> addData = new ArrayList<Map<String, Object>>(); for (Map<String, Object> m1 : newData) { for (Map.Entry<String, Object> e1 : m1.entrySet()) { boolean exist = false; for (Map<String, Object> m2 : data) { if (m2.containsKey(e1.getKey())) { exist = true; m2.put(e1.getKey(), Integer.valueOf((String)m2.get(e1.getKey())) + Integer.valueOf((String)e.getValue())); break; } } if (! exist) { map = new Map<String, Object>(); map.put(e1.getKey(), e1.getValue()); addData.add(map); } } } data.addAll(addData); } }