JAVA study notes _List data deduplication

Remove duplicate values ​​in List collection (four easy-to-use methods)

Most of them use two methods,
one is to traverse the list collection and then assign it to another list collection, and the other
is to assign it to the set collection and then return it to the list collection.
But after assigning to the set collection, since the set collection is unordered, the original order is disrupted

    //set集合去重,不打乱顺序
    public static void main(String[] args){
         List<String> list  =   new  ArrayList<String>(); 
         list.add("aaa");
         list.add("bbb");
         list.add("aaa");
         list.add("aba");
         list.add("aaa");

         Set set = new  HashSet(); 
         List newList = new  ArrayList(); 
         for (String cd:list) {
            if(set.add(cd)){
                newList.add(cd);
            }
        }
         System.out.println( "去重后的集合: " + newList); 
      }
    //遍历后判断赋给另一个list集合
     public static void main(String[] args){
         List<String> list  =   new  ArrayList<String>(); 
         list.add("aaa");
         list.add("bbb");
         list.add("aaa");
         list.add("aba");
         list.add("aaa");

         List<String> newList = new  ArrayList<String>(); 
         for (String cd:list) {
            if(!newList.contains(cd)){
                newList.add(cd);
            }
        }
         System.out.println( "去重后的集合: " + newList); 
      }
  //set去重
    public static void main(String[] args){
         List<String> list  =   new  ArrayList<String>(); 
         list.add("aaa");
         list.add("bbb");
         list.add("aaa");
         list.add("aba");
         list.add("aaa");

         Set set = new  HashSet(); 
         List newList = new  ArrayList(); 
         set.addAll(list);
         newList.addAll(set);

         System.out.println( "去重后的集合: " + newList); 
     }
        //set去重(缩减为一行)
        public static void main(String[] args){
             List<String> list  =   new  ArrayList<String>(); 
             list.add("aaa");
             list.add("bbb");
             list.add("aaa");
             list.add("aba");
             list.add("aaa");

             List newList = new ArrayList(new HashSet(list)); 
             System.out.println( "去重后的集合: " + newList); 
         }

Hashset is not sorted. Another method is to use treeset, remove duplicates and arrange them in natural order, and change hashset to treeset. (The original order is changed, it's just in alphabetical order)

//去重并且按照自然顺序排列
List newList = new ArrayList(new TreeSet(list)); 

https://www.cnblogs.com/YLQBL/p/6527077.html

public static void getlist(List<String> arr) {
        ArrayList<String> result = new ArrayList<String>();
        for (Object s : arr) {
            if (Collections.frequency(result, s)<1)
                result.add((String) s);
        }
        System.out.println(result);
    }
针对方法getList的小说明:

frequency(Collection<?>, Object) 方法用于获取
所指定元素集合collection等于指定对象object中的数量。

if (Collections.frequency(result, s)<1)
                result.add((String) s);

的意思是如果对象s在集合result中出现的次数小于1(无s元素),就将它添加进入集合result

Method of removing duplicate data from List collection in Java
1. Loop through all elements in list and then delete duplicates

public   static   List  removeDuplicate(List list)  {       
  for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {       
      for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {       
           if  (list.get(j).equals(list.get(i)))  {       
              list.remove(j);       
            }        
        }        
      }        
    return list;       
}  
for (int i = 0; i < mList.size()-1; i++) {  
    for (int j = mList.size()-1; j > i; j--) {  
        if (mList.get(j).destip.equals(mList.get(i).destip)) {  
            mList.remove(j);  
        }  
    }  
}  

2. Kick out duplicate elements through HashSet

public static List removeDuplicate(List list) {   
    HashSet h = new HashSet(list);   
    list.clear();   
    list.addAll(h);   
    return list;   
}   

3. Delete duplicate elements in ArrayList and keep order

// 删除ArrayList中重复元素,保持顺序     
 public static void removeDuplicateWithOrder(List list) {    
    Set set = new HashSet();    
     List newList = new ArrayList();    
   for (Iterator iter = list.iterator(); iter.hasNext();) {    
         Object element = iter.next();    
         if (set.add(element))    
            newList.add(element);    
      }     
     list.clear();    
     list.addAll(newList);    
    System.out.println( " remove duplicate " + list);    
 }   

4. Traverse the objects in the list, use list.contain(), if they do not exist, put them into another list collection

public static List removeDuplicate(List list){  
        List listTemp = new ArrayList();  
        for(int i=0;i<list.size();i++){  
            if(!listTemp.contains(list.get(i))){  
                listTemp.add(list.get(i));  
            }  
        }  
        return listTemp;  
    }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723663&siteId=291194637