List<Map<String,String>>操作(遍历,比较)
1.List<Map<String,String>>的遍历:
Map<String,String> map = new HashMap<String, String>(); map.put("1", "fds"); map.put("2", "valu"); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); list.add(map); for (Map<String, String> m : list){ for (String k : m.keySet()){ System.out.println(k + " : " + m.get(k)); } }
2.两个List<Map<String,String>>相互比较,有相同数据时某些字段相加减
inOrderList与outOrderList放自己的所比较的数据,name,number,id为map的key值
for (int i = 0; i < inOrderList.size(); i++) {//循环获取入库数据 String number1 = String.valueOf(inOrderList.get(i).get("number")); String depotName1 = inOrderList.get(i).get("name"); String proId1 = inOrderList.get(i).get("id"); for (int j = 0; j < outOrderList.size(); j++) {//循环获取出库数据 String number2 = String.valueOf(outOrderList.get(j).get("number")); String depotName2 = outOrderList.get(j).get("name"); String proId2 = outOrderList.get(j).get("id"); if (depotName1.equals(depotName2) && proId1.equals(proId2)) { Integer sum =Integer.parseInt(number1) - Integer.parseInt(number2);//判断是否为同一条数据,同条数据数量相减 inOrderList.get(i).put("number", sum + ""); } } }
3.单个List<Map<String,String>>相互比较,相同数据相加(转)
List<Map<String, String>> l = new ArrayList<Map<String,String>>(); Map<String, String> m1 = new HashMap<String, String>(); m1.put("count", "1"); m1.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); m1.put("province", "江苏省"); m1.put("date", "2014-09-23 10:13:39"); m1.put("channel", "东环大润发"); m1.put("city", "苏州市"); l.add(m1); Map<String, String> m2 = new HashMap<String, String>(); m2.put("count", "1"); m2.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); m2.put("province", "江苏省"); m2.put("date", "2014-09-23 10:13:39"); m2.put("channel", "东环大润发"); m2.put("city", "泰州市"); l.add(m2); Map<String, String> m3 = new HashMap<String, String>(); m3.put("count", "1"); m3.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); m3.put("province", "江苏省"); m3.put("date", "2014-09-23 10:13:39"); m3.put("channel", "东环大润发"); m3.put("city", "泰州市"); l.add(m3); List<Map<String, String>> countList = new ArrayList<Map<String,String>>();//用于存放最后的结果 for (int i = 0; i < l.size(); i++) { String count = l.get(i).get("count"); String name = l.get(i).get("name"); String province = l.get(i).get("province"); String date = l.get(i).get("date"); String channel = l.get(i).get("channel"); String city = l.get(i).get("city"); int flag = 0;//0为新增数据,1为增加count for (int j = 0; j < countList.size(); j++) { String count_ = countList.get(j).get("count"); String name_ = countList.get(j).get("name"); String province_ = countList.get(j).get("province"); String date_ = countList.get(j).get("date"); String channel_ = l.get(j).get("channel"); String city_ = countList.get(j).get("city"); if (count.equals(count_)&& name.equals(name_)&& province.equals(province_)&& date.equals(date_)&& channel.equals(channel_)&& city.equals(city_)) { int sum = Integer.parseInt(l.get(i).get("count")) + Integer.parseInt(countList.get(j).get("count")); countList.get(j).put("count", sum + ""); flag = 1; continue; } } if (flag == 0) { countList.add(l.get(i)); } } for (Map<String, String> map : countList) { System.out.println(map); } }
public class Test { /** * @param args */ public static void main(String[] args) { List<Map<String, String>> list = new ArrayList<Map<String,String>>(); Map<String,String> map1 = new HashMap<String,String>(); map1.put("count", "1"); map1.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); map1.put("province", "江苏省"); map1.put("date", "2014-09-23 10:13:39"); map1.put("channel", "东环大润发"); map1.put("city", "苏州市"); list.add(map1); Map<String,String> map2 = new HashMap<String,String>(); map2.put("count", "1"); map2.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); map2.put("province", "江苏省"); map2.put("date", "2014-09-23 10:13:39"); map2.put("channel", "东环大润发"); map2.put("city", "泰州市"); list.add(map2); Map<String,String> map3 = new HashMap<String,String>(); map3.put("count", "1"); map3.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); map3.put("province", "江苏省"); map3.put("date", "2014-09-23 10:13:39"); map3.put("channel", "东环大润发"); map3.put("city", "泰州市"); list.add(map3); Map<String,String> map4 = new HashMap<String,String>(); map4.put("count", "1"); map4.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); map4.put("province", "江苏省"); map4.put("date", "2014-09-23 10:13:39"); map4.put("channel", "东环大润发"); map4.put("city", "苏州市"); list.add(map4); System.out.println(mergeList(list)); } /** * 合并相同的Map * @param list * @return */ public static List<Map<String,String>> mergeList(List<Map<String,String>> list){ List<Map<String,String>> retList = new ArrayList<Map<String,String>>(); for(int i=0;i<list.size();i++){ Map<String,String> map = list.get(i); String name = map.get("name"); String city = map.get("city"); String count = map.get("count"); Map<String,String> newMap=isExistSame(i,name,city,count,list); if(null==newMap){ retList.add(map); } else{ list.remove(map.get(i)); retList.add(newMap); } } return retList; } private static Map<String,String> isExistSame(int i,String name,String city,String count,List<Map<String,String>> list){ Map<String,String> newMap = null; for(int j=i+1;j<list.size();j++){ Map<String,String> innerMap = list.get(j); String name1 = innerMap.get("name"); String city1 = innerMap.get("city"); String count1 = innerMap.get("count"); if(name.equals(name1)&&city.equals(city1)){ newMap = new HashMap<String,String>(); newMap.put("count", ""+(Integer.parseInt(count)+Integer.parseInt(count1))); Iterator<String> keyIterator = innerMap.keySet().iterator(); while(keyIterator.hasNext()){ String key = keyIterator.next(); if(!key.equals("count")){ newMap.put(key, innerMap.get(key)); } } list.remove(j); } } return newMap; } }
ublic static void main(String[] args) { List<Map<String, String>> list = new ArrayList<Map<String,String>>(); boolean m = true; for(int i=0;i<10000;i++){ HashMap<String, String> map = new HashMap<String, String>(); map.put("count", "1"); map.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿"); map.put("province", "江苏省"); map.put("date", "2014-09-23 10:13:39"); map.put("channel", "东环大润发"); map.put("city", m ? "苏州市":"泰州市"); m = !m; list.add(map); } System.out.println(System.currentTimeMillis()); for(int i=list.size();--i>-1;){ Map<String, String> src = list.get(i); int same = 0; for(int j=i-1;j>-1;j--){ Map<String, String> tar = list.get(j); if(equals(src, tar)){ int count = Integer.parseInt(src.get("count")) +Integer.parseInt(tar.get("count")); src.put("count", count+""); list.remove(j); same++; } } i -=same; } System.out.println(list); System.out.println(System.currentTimeMillis()); } public static boolean equals(Map<String, String> src,Map<String, String> tar){ Set<Entry<String,String>> entrySet = src.entrySet(); String srcCount = src.remove("count");//count不参与比较 String srcValue = null,tarValue = null; for (Iterator<Entry<String,String>> it = entrySet.iterator(); it.hasNext();) { Entry<String, String> entry = it.next(); srcValue = entry.getValue(); tarValue = tar.get(entry.getKey()); if(srcValue.length()!=tarValue.length()||!srcValue.equals(tarValue)){ src.put("count", srcCount); return false; } } src.put("count", srcCount); return true; }