1. 程式人生 > >List去重複元素

List去重複元素

list:[{"ss":123,"dd":234,"dd":234},{"mm":99,"ff":oo,"ff":oo}]

這種形式的:

List<Map<String,Object>> newList 
= new ArrayList<Map<String,Object>>();
        Set<Set<String>> keysSet = new HashSet<Set<String>>();
        for(Map<String, Object> map : list){
            Set<String> keys = map.keySet();
            int beforeSize = keysSet.size();
            keysSet.add(keys);
            int afterSize = keysSet.size();
            if(afterSize == beforeSize + 1){
                newList.add(map);
            }
        }
return newList;

list:[{groupId=122}, {groupId=124}, {groupId=135}, {groupId=91},

 {groupId=109}, {groupId=124}, {groupId=135}, {groupId=126},

{groupId=127}, {groupId=133}, {groupId=134}, {groupId=151}]

 這種形式的:

List<Map<String,Object>> newList 
= new ArrayList<Map<String,Object>>();
        Set<Map<String,Object>> mapSet
       = new HashSet<Map<String,Object>>();
        for(Map<String, Object> map : list) {
            mapSet.add(map);
        }
Iterator<Map<String, Object>> iteratormap=mapSet.iterator();
   while(iteratormap.hasNext()){
         newList.add(iteratormap.next());
   }
return newList;