public static void main(String args[]){
Map<String, String> mapNew = new HashMap<String, String>();
mapNew.put("count", "100");
mapNew.put("suggest", "天美意");
mapNew.put("type", "1");
Map<String, String> map1 = new HashMap<String, String>();
map1.put("count", "90");
map1.put("suggest", "天美意女鞋");
map1.put("type", "1");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("count", "80");
map2.put("suggest", "天美意");
map2.put("type", "3");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("count", "50");
map3.put("suggest", "天美意鞋");
map3.put("type", "3");
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
list.add(mapNew);
list.add(map1);
list.add(map2);
list.add(map3);
List<Map<String,String>> tmpList=new ArrayList<Map<String,String>>();
Set<String> keysSet = new HashSet<String>();
for(Map<String, String> map : list){
String keys = map.get("suggest");
int beforeSize = keysSet.size();
keysSet.add(keys);
int afterSize = keysSet.size();
if(afterSize == beforeSize + 1){
tmpList.add(map);
}
}
}
Map<String, String> mapNew = new HashMap<String, String>();
mapNew.put("count", "100");
mapNew.put("suggest", "天美意");
mapNew.put("type", "1");
Map<String, String> map1 = new HashMap<String, String>();
map1.put("count", "90");
map1.put("suggest", "天美意女鞋");
map1.put("type", "1");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("count", "80");
map2.put("suggest", "天美意");
map2.put("type", "3");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("count", "50");
map3.put("suggest", "天美意鞋");
map3.put("type", "3");
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
list.add(mapNew);
list.add(map1);
list.add(map2);
list.add(map3);
List<Map<String,String>> tmpList=new ArrayList<Map<String,String>>();
Set<String> keysSet = new HashSet<String>();
for(Map<String, String> map : list){
String keys = map.get("suggest");
int beforeSize = keysSet.size();
keysSet.add(keys);
int afterSize = keysSet.size();
if(afterSize == beforeSize + 1){
tmpList.add(map);
}
}
}
期望去重suggest返回一个“天美意”,最后只保留第一次出现的“天美”意,也就是mapNew,map2则给去掉了
list.size()=4 去重后tmpList.size()=3