Java删除2个ArrayList对象之间的公共元素

时间:2012-07-25 09:40:02

标签: java arraylist

我有一个Java类

class Students{
    private String fName;   
    private String lName;
    private String uName;


    public Students(String fName, String lName, String uName) {
            this.fName = fName;
            this.lName = lName;
            this.uName = uName;
     }      

    public String getuName() {
        return uName;
    }
    public void setuName(String uName) {
        this.uName = uName;
    }
    public String getfName() {
        return fName;
    }
    public void setfName(String fName) {
        this.fName = fName;
    }
    public String getlName() {
        return lName;
    }
    public void setlName(String lName) {
        this.lName = lName;
    }

}

我也是用

来打电话的
public class TestClass {
public static void main(String[] args) {

    Students students1 = new Students("xyz","abc","xyzAbc");
    Students students2 = new Students("poi","son","poison");
    Students students3 = new Students("yog","jos","yogjos");

    Students students4 = new Students("xyz","abc","xyzAbc");
    Students students5 = new Students("pon","son","xyzAbc");
    Students students6 = new Students("yog","jos","someotherUName");
    Students students7 = new Students("yog","jos","someotherUName2");

    List studentList1 = new ArrayList();
    List studentList2 = new ArrayList();

    studentList1.add(students1);
    studentList1.add(students2);
    studentList1.add(students3);

    studentList2.add(students4);
    studentList2.add(students5);
    studentList2.add(students6);
}
}

现在我想要一个过滤列表,它只包含唯一的" uName"值。 因此,我想要" uName"之间的比较。每个列表的字段并删除常见的字段。

最后,我想要studentList1和studentList2的2个文件列表。

我读到了removeAll方法,但它似乎与List of Integer / String数据一起使用,而不是与对象列表一样(在我的例子中)。

请建议。

7 个答案:

答案 0 :(得分:3)

您可以将列表设置为然后(如果需要)将其取回:

new ArrayList(new HashSet<String>(list))创建的列表仅包含源列表中的唯一元素。

答案 1 :(得分:3)

1。如果您希望每个列表都具有唯一 uName值,那么您可以使用TreeSet中的java.util.Collection来自Comparator界面 java.util.Comparator

2. 如果您想要合并列表并拥有唯一的uName ,请将两个列表合并,然后使用TreeSet和{{1} }。

3。 Comparator可以灵活地以多种方式进行比较 ......

答案 2 :(得分:2)

如果removeAll中的对象正确实施List,您仍然可以使用equals()

AbstractCollection是大多数List实施(包括ArrayList)的基础,在contains()中使用its implementation of removeAllArrayList的{​​{3}}依赖indexOf(),最后使用equals()

您可以在equals()课程中实施Student,以指定当Student字段相等时,uName与其他字段相等。

请注意equals具有关联的语义(请参阅其implementation of contains),在选择实现方法时应该小心。考虑两个学生实例在uName相等时是否真正代表同一个学生。在我看来,这听起来像是一个非常具体的要求,如何对这些东西进行排序,不应该影响类的语义。

使用javadoc@AlexR方法,你会好得多。

答案 3 :(得分:2)

首先,您应该输入清单:

List<Students> studentList1 = new ArrayList<Students>();

其次,在hashCode()班级上实施equals()Students,并uName代表public boolean equals(Object o) { return o instanceof Students && ((Students)o).uName.equals(uName); } public int hashCode() { return uName.hashCode(); }

Set

现在removeAll()可以正常使用。


另一种选择是使用equals(),它只允许由Set<Students> students = new HashSet<Students>(); 方法确定的唯一值。如果您将上述方法添加到您的课程中,您可以这样做:

uName

然后添加您喜欢的内容,其中只会有独特的Student学生。


顺便说一下,你应该用单数命名你的班级 - 即Students而不是{{1}}。

答案 4 :(得分:0)

您也可以尝试使用apache commons CollectionUtils.disjunction。 (链接:http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html#disjunction(java.util.Collection,java.util.Collection))

答案 5 :(得分:0)

您的班级学生应该覆盖方法hashcodeequals,因此您的对象可以按名称唯一。 Here你可以阅读如何做到这一点。

另请注意,在某些集合中维护的所有对象都应该覆盖这些方法,因此您始终知道它们是如何比较的。

在下一步中,您可以使用removeAll方法或使用Set的解决方案,这取决于您: - )

设置示例

List<String> list = new ArrayList(new HashSet<String>(studentList)); //这是您的唯一列表

或者您可以使用自定义Comparator而不是覆盖hashcode和equals但是如果您希望您的学生始终是唯一的,则应该覆盖它们

答案 6 :(得分:0)

完整的解决方案如下:

Class Student{


.....
......
.....

   public boolean equals(Object o) {
       return o instanceof Students && ((Students)o).uName.equals(uName);
   }

   public int hashCode() {
       return uName.hashCode();
   }




}


Set studentList1 = new hashSet();
Set studentList2 = new hashSet();

将您的元素放在这些集合中。

此外,如果您想在两个集合中使用唯一的非匹配元素。然后写如下。

studentList1.removeAll(studentList2 );