2013-07-30 45 views
0

我有一個ArrayList填充了POJO,我想刪除所有具有重複變量的POJO。這是POJO:刪除填充了POJO的ArrayList中的重複項

public static class UrlStore { 

    public String url; 
    public String data; 

    public UrlStore(String url) { 
     this.url = url; 
    } 

    public void setData(String data) { 
     this.data = data; 
    } 
} 

我的方式來刪除重複的「URL」 - 變量在ArrayList<UrlStore>是通過列表進行迭代,並刪除這些重複。據我所知,我可以簡單地使用Set來做到這一點,但我無法弄清楚如何在包含POJO的ArrayList中使用它。或者你有更好的方法?

感謝您的任何建議!

+3

添加'equals'和'hashCode'方法您的POJO ,那麼只需從'List'(用'Set.addAll')創建一個'Set',它會自動刪除重複項。 – Alex

+1

只需將列表轉換爲設置並享受即可。 –

回答

4
package test.urlstore; 

import java.util.ArrayList; 
import java.util.HashSet; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Set; 


public class DuplicateDemo{ 

    public static void main(String[] args) throws Exception { 
     List<UrlStore> urlStores = new ArrayList<UrlStore>(); 
     UrlStore usg = new UrlStore("google"); 
     UrlStore usy = new UrlStore("yahoo"); 
     UrlStore usb = new UrlStore("bing"); 
     UrlStore usa = new UrlStore("ask"); 
     UrlStore usd = new UrlStore("duckduckgo"); 


     usg.setData("mail"); 
     urlStores.add(usg); 
     usg = new UrlStore("google"); 
     usg.setData("search"); 
     urlStores.add(usg); 
     usg = new UrlStore("google"); 
     usg.setData("doc"); 
     urlStores.add(usg); 
     usg = new UrlStore("google"); 
     usg.setData("search"); 
     urlStores.add(usg); 
     usg = new UrlStore("google"); 
     usy.setData("search"); 
     urlStores.add(usy); 
     usy.setData("search"); 
     urlStores.add(usy); 
     usb.setData("search"); 
     urlStores.add(usb); 
     usb.setData("search"); 
     urlStores.add(usb); 
     usa.setData("search"); 
     urlStores.add(usa); 
     usd.setData("search"); 
     urlStores.add(usd); 

     System.out.println("before removing duplicates"); 
     // before removing duplicates 
     for (Iterator iterator = urlStores.iterator(); iterator.hasNext();) { 
      UrlStore urlStore = (UrlStore) iterator.next(); 
      System.out.println(urlStore.toString()); 
     } 

     System.out.println("\n\nafter removing duplicates"); 
     //removing duplicates 
     Set<UrlStore> uniqueUrlStores = new HashSet<UrlStore>(urlStores); 

     //After removing duplicates 
     for (Iterator iterator = uniqueUrlStores.iterator(); iterator.hasNext();) { 
      UrlStore urlStore = (UrlStore) iterator.next(); 
      System.out.println(urlStore.toString()); 

     } 

    } 



    static class UrlStore { 

     public String url; 
     public String data; 

     public UrlStore(String url) { 
      this.url = url; 
     } 

     public void setData(String data) { 
      this.data = data; 
     } 

     @Override 
     public String toString() { 
      return "UrlStore [url=" + url + ", data=" + data + "]"; 
     } 

     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + ((data == null) ? 0 : data.hashCode()); 
      result = prime * result + ((url == null) ? 0 : url.hashCode()); 
      return result; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (getClass() != obj.getClass()) 
       return false; 
      UrlStore other = (UrlStore) obj; 
      if (data == null) { 
       if (other.data != null) 
        return false; 
      } else if (!data.equals(other.data)) 
       return false; 
      if (url == null) { 
       if (other.url != null) 
        return false; 
      } else if (!url.equals(other.url)) 
       return false; 
      return true; 
     } 


    } 
} 
+0

哇..非常感謝工作!有點toooo,但它完美的作品:) – Crayl

9

您可以覆蓋POJO中的equals()hashCode()方法,並將List<UrlStore>傳遞給Set

List<UrlStore> listOfUrlStore = ...... 
Set<UrlStore> foo = new HashSet<UrlStore>(listOfUrlStore); 

我用一個簡單的HashSet這裏,正確執行Set取決於您的要求。您甚至可以將Set轉換回List

listOfUrlStore.clear(); 
listOfUrlStore.addAll(foo); 
1

不要忘記重寫您POJO的equals方法。 這樣做完成後,您可以使用contains方法List在添加之前檢查您的對象是否在列表中。
對於Set,您不必使用它的清單,它取代的清單。您可以從ListaddAll(Collection c)

2

重載hashCode()和equals()在你的POJO,然後填充Set

List<URLStore>() list = //Your list; 
Set<URLStore> set =new HashSet<URLStore>(list);