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

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;
    }
}

最佳答案

试试这个解决方案:

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;
}

07-25 21:11