从列表中删除重复的集

时间:2014-02-14 08:39:01

标签: java collections

我正在尝试从ArrayList中删除重复的自定义对象集。下面是我编写的代码,它使用toString表示自定义EmployeeObj进行比较。你能否建议采取其他方法?

package com.collections;

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

public class DupSetInsideList {
    public static void main(String[] args) {
        List<Set<EmployeeObj>> list = new ArrayList<Set<EmployeeObj>>();

        Set<EmployeeObj> set1 = new HashSet<EmployeeObj>();
        Set<EmployeeObj> set2 = new HashSet<EmployeeObj>();
        Set<EmployeeObj> set3 = new HashSet<EmployeeObj>();

        list.add(set1);
        list.add(set2);
        list.add(set3);

        EmployeeObj empObj1 = new EmployeeObj(1, "Nikhil");
        EmployeeObj empObj2 = new EmployeeObj(2, "Rakesh");
        EmployeeObj empObj3 = new EmployeeObj(3, "Kunal");

        set1.add(empObj1);
        set1.add(empObj2);

        set2.add(empObj1);
        set2.add(empObj2);

        set3.add(empObj1);
        set3.add(empObj2);
        set3.add(empObj3);

        System.out.println("List with duplicaes: " + list);
        //Output: List with duplicaes: [[1=Nikhil, 2=Rakesh], [1=Nikhil, 2=Rakesh], [3=Kunal, 1=Nikhil, 2=Rakesh]]

        //Remove duplicates
        List<Set<EmployeeObj>> nonDupList = new ArrayList<Set<EmployeeObj>>();
        for(Set<EmployeeObj> obj1:list) {
            if(!nonDupList.contains(obj1)) {
                nonDupList.add(obj1);
            }
        }
        System.out.println("List without duplicates: " + nonDupList);
        //List without duplicates: [[1=Nikhil, 2=Rakesh], [3=Kunal, 1=Nikhil, 2=Rakesh]]
    }
}

class EmployeeObj {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }


    public EmployeeObj(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.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;
        EmployeeObj other = (EmployeeObj) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return id + "=" + name;
    }
}

4 个答案:

答案 0 :(得分:1)

如果您想要保证元素唯一性的列表行为,请使用LinkedHashSet代替ArrayList

如果您必须使用ArrayList(如在学生练习中),请通过先检查唯一性然后调用{{1}来扩展它,覆盖addaddAll方法} resp。 super.add然后使用新类代替super.addAll

答案 1 :(得分:1)

Set接口指定equals方法,如下所示:

  

将指定对象与此集进行相等性比较。如果指定的对象也是一个集合,则返回true,两个集合具有相同的大小,并且指定集合的​​每个成员都包含在此集合中(或者等效地,此集合的每个成员都包含在指定的集合中)。 p>

因此,为了创建一个不包含重复项的列表(即使列表中的元素是集合),也可以简单地编写

    //Remove duplicates
    List<Set<EmployeeObj>> nonDupList = 
        new ArrayList<Set<EmployeeObj>>(
            new LinkedHashSet<Set<EmployeeObj>>(list));

答案 2 :(得分:0)

将集合放入集合中,它们将被移除,就像魔术一样!

答案 3 :(得分:0)

试试这个解决方案:

private boolean equals(Set elements, Set elements2) {
    return elements != null && elements.equals(elements2);
}

private List<Set> removeDuplicates(List<Set> from) {
    List<Set> noDuplicates = new ArrayList<Set>();

    for (Set possibleDuplicate : from) {
        boolean alreadyInNoDuplicatesList = false;
        for (Set elementFromNoDuplicateList : noDuplicates) {
            if (equals(elementFromNoDuplicateList, possibleDuplicate)) {
                alreadyInNoDuplicatesList = true;
                break;
            }
        }
        if (!alreadyInNoDuplicatesList) {
            noDuplicates.add(possibleDuplicate);
        }
    }

    return noDuplicates;
}