Java删除2个对象ArrayList之间的公共元素 [英] Java Remove common elements between 2 ArrayList of objects
问题描述
我有一个 Java 类
I have a Java class as
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"字段并删除常见的字段.
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 个过滤列表.
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).
请提出建议.
推荐答案
如果List
中的Objects实现了equals()<,你仍然可以使用
removeAll
/code> 正确.
You can still use removeAll
if the Objects in the List
implement equals()
properly.
AbstractCollection
,这是大多数 List
实现(包括 ArrayList
)的基础,使用 contains()
在 removeAll
的实现.ArrayList
的 contains
的实现依赖于 indexOf()
,最后使用equals()
.
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()
.
你可以在你的 Student
类中实现 equals()
来指定一个 Student
当且仅当他们的 等于另一个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.
请注意,equals
具有相关的语义(请参阅其 javadoc),并且在选择如何实现它时应该小心.考虑当两个学生实例的 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 uName
s 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 的方法.
这篇关于Java删除2个对象ArrayList之间的公共元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!