如何比较 Java 中包含同一类对象的两个 Arraylist?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16960479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two Arraylists which contain objects of the same class in Java?
提问by user2077648
I have 2 lists of different sizes which belong to the same class Officer
which has attributes:
我有 2 个不同大小的列表,它们属于相同的class Officer
属性:
Integer id;
Integer level;
String role;
I want to compare these two lists list1 and list2 so that I can generate two new lists insertLst and deleteList.
我想比较这两个列表 list1 和 list2,以便我可以生成两个新列表 insertLst 和 deleteList。
Both list 1 and list2 contain unique elements.
列表 1 和列表 2 都包含唯一元素。
insertLst: This list contains items in list1 and not in list2.
insertLst:此列表包含 list1 中的项目而不是 list2 中的项目。
deleteList: This list contains items in list2 and not in list1.
deleteList:此列表包含 list2 中的项目而不是 list1 中的项目。
I have no idea how this should be done , might be using The org.apache.commons.collections.CollectionUtils API .
我不知道这应该如何完成,可能正在使用 org.apache.commons.collections.CollectionUtils API。
Could you suggest me a java code for generating these two lists insertLst and deleteList?
你能给我推荐一个java代码来生成这两个列表insertLst和deleteList吗?
回答by Tala
If you want to use org.apache.commons.collections.CollectionUtils then you should use subtract method.
如果你想使用 org.apache.commons.collections.CollectionUtils 那么你应该使用减法。
Collection<Officer> insertList= CollectionUtils.subtract(list1, list2);
Collection<Officer> deleteList = CollectionUtils.subtract(list2, list1);
or you could write it like this (passing empty lists that are filled in method) not to add another library to your dependencies:
或者你可以这样写(传递填充在方法中的空列表)不要向你的依赖项添加另一个库:
static <T> void process(List<T> list1, List<T> list2, List<T> insertList, List<T> deleteList) {
for (T t: list1) {
if (!list2.contains(t)) {
insertList.add(t);
}
}
for (T t: list2) {
if (!list1.contains(t)) {
deleteList.add(t);
}
}
}
Don't forget tooverride equals and hashcode methods for you class Officer in both cases. Please refer herefor explanation
在这两种情况下,不要忘记为您的类官员覆盖 equals 和 hashcode 方法。请参考这里的解释
回答by jlordo
Make sure your Officer
class overrides the equals()
(and hashCode()
) method and it will be really easy:
确保您的Officer
类覆盖equals()
(and hashCode()
) 方法,这将非常简单:
List<Officer> list1 = ... // your list1
List<Officer> list2 = ... // your list2
List<Officer> insertLst = new ArrayList<>(list1);
insertLst.removeAll(list2);
List<Officer> deleteList= new ArrayList<>(list2);
deleteList.removeAll(list1);
回答by jlordo
final List<Officer> one = new ArrayList<Officer>();
...
final List<Officer> two = new ArrayList<Officer>();
...
final List<Officer> insertList = new ArrayList<Officer>(one);
insertList.removeAll(two);
final List<Officer> removeList = new ArrayList<Officer>(two);
removeList.removeAll(one);
Edit: As jlordo said, make sure you override hashCode and Equals
编辑:正如 jlordo 所说,确保你覆盖 hashCode 和 Equals
回答by MaVRoSCy
You have to loop through the two lists ....
你必须遍历这两个列表......
One way to do this could be this one:
做到这一点的一种方法可能是这样的:
ArrayList <Officer> list1= ....
ArrayList <Officer> list2= ....
ArrayList <Officer> insertList= new ArrayList <Officer>();
ArrayList <Officer> deleteList= new ArrayList <Officer>();
for(Officer x:list1){
for(Officer y:list2){
//business logic here
if(x.id==y.id){
insertList.add(x); //example code to add an Object in a List
}
}
}
The business logic part is up to you... you can compare data between the two lists and decide what goes where...
业务逻辑部分由您决定...您可以比较两个列表之间的数据并决定什么去哪里...
Hope this helps
希望这可以帮助