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;
}
