简体   繁体   English

如何比较两个包含Java中同一类对象的Arraylists?

[英]How to compare two Arraylists which contain objects of the same class in Java?

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吗?

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 to override equals and hashcode methods for you class Officer in both cases. 在这两种情况下, 不要忘记为您的类官员重写equals和hashcode方法。 Please refer here for explanation 请参考这里的解释

Make sure your Officer class overrides the equals() (and hashCode() ) method and it will be really easy: 确保你的Officer类重写了equals() (和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);
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

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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM