2 ArrayList的对象之间资源下载删除共同要素 [英] Java Remove common elements between 2 ArrayList of objects

查看:129
本文介绍了2 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;
    }

}

此外,我把这个用

Also I call this using

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字段之间的对比,并删除常见的。

Now I want a filtered list which would contain only unique "uName" values. Thus I want the comparision between "uName" field of each list and remove common ones.

最后我想对studentList1和studentList2 2 filetered列表。

At the end I would want 2 filetered list for studentList1 and studentList2.

我读到RemoveAll方法,但它似乎与整数/字符串的数据列表,而不是与对象列表(在我的情况)上班。

I read about the removeAll method, but it seems to work with List of Integer/String data and not with List of Objects (as in my case).

请建议。

推荐答案

您还可以使用的removeAll 如果在列表中的对象实施等于()正常。

You can still use removeAll if the Objects in the List implement equals() properly.

类AbstractCollection ,这是最善良的列表实现(包括基本的ArrayList )使用<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/util/AbstractCollection.java#AbstractCollection.contains%28java.lang.Object%29\"相对=nofollow> 包含() 在<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/util/AbstractCollection.java#AbstractCollection.removeAll%28java.util.Collection%29\"相对=nofollow>及其实施的removeAll 的。 的ArrayList 的<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java#ArrayList.contains%28java.lang.Object%29\"相对=nofollow>实施包含 依赖于的indexOf(),其中最后使用等于()

AbstractCollection, which is the base for most kind of List implementations (including ArrayList) uses contains() in its implementation of removeAll. ArrayList's implementation of contains relies on indexOf(), which lastly uses equals().

您可以实施等于()学生类来指定一个学生等于另一个,当且仅其的uname 字段是相等的。

You could implement equals() in your Student class to specify that an Student is equal to another if and only their uName fields are equal.

请注意,等于有相关的语义(见它的<一个href=\"http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#equals%28java.lang.Object%29\"相对=nofollow>的javadoc ),你选择如何实现它的时候要慎重。试想,如果二个学生情况真的重新present同一个学生时,他们的的uname 是相等的。在我看来,这听起来像如何把这些理清头绪一个非常具体的要求,并应当不会影响类的语义。

Please note that equals has associated semantics (see its javadoc), and you should be careful when choosing how to implement it. Consider if two student instances really represent the same student when their uNames are equal. In my opinion, this sounds like a very specific requirement of how to sort these things out and should not impact the semantics of the class.

您会与 @AlexR 或的 @ KumarVivekMitra 的做法。

这篇关于2 ArrayList的对象之间资源下载删除共同要素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆