Java 删除ArrayList中重复元素,保持顺序

时间:2023-11-10 08:08:08

// 删除ArrayList中重复元素,保持顺序     
     public static List<Map<String, Object>> removeDuplicateWithOrder(List<Map<String, Object>> list) {    
        Set<Map<String, Object>> set = new HashSet<Map<String, Object>>();    
        List<Map<String, Object>> newList = new ArrayList<Map<String,Object>>();    
        for (Iterator<Map<String, Object>> iter = list.iterator(); iter.hasNext();) {    
             Map<String, Object> element =iter.next();    
             if (set.add(element))    
                newList.add(element);    
          }     
         list.clear();    
         list.addAll(newList);
         return list;
     }