0

我有一个 ArrayList 填充的 POJO,我想删除所有具有重复变量的 POJO。这是 POJO:

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>方法是遍历列表并删除这些重复项。有人对我说,我可以简单地使用 aSet来执行此操作,但我不知道如何将它与包含 POJO 的 ArrayList 一起使用。或者你有更好的方法吗?

感谢您的任何建议!

4

4 回答 4

11

您可以覆盖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);   
于 2013-07-30T07:24:33.020 回答
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;
        }


    }   
}
于 2013-07-30T07:48:33.867 回答
2

覆盖 POJO 中的 hashCode() 和 equals() ,然后:

List<URLStore>() list = //Your list;
Set<URLStore> set =new HashSet<URLStore>(list);
于 2013-07-30T07:26:58.310 回答
1

不要忘记覆盖equalsPOJO 的方法。完成后,您可以使用contains方法List来检查您的对象是否在列表中,然后再添加它。
对于Set,您不必将其列表一起使用,它会替换列表。您可以Set从 a填充 aListaddAll(Collection c)

于 2013-07-30T07:26:31.077 回答