1

I have 2 lists of different sizes which belong to the same class Officer which has attributes:

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.

Both list 1 and list2 contain unique elements.

insertLst: This list contains items in list1 and not in list2.

deleteList: This list contains items in list2 and not in list1.

I have no idea how this should be done , might be using The org.apache.commons.collections.CollectionUtils API .

Could you suggest me a java code for generating these two lists insertLst and deleteList?

3
  • Make a copy of your original lists and use the removeAll() method on each list. Make sure that your class has overrided the equals() method. Commented Jun 6, 2013 at 11:09
  • Are all elements of the list unique? Commented Jun 6, 2013 at 11:12
  • Yes the elements of the list are unique Commented Jun 6, 2013 at 11:27

4 Answers 4

3

If you want to use org.apache.commons.collections.CollectionUtils then you should use subtract method.

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. Please refer here for explanation

Sign up to request clarification or add additional context in comments.

Comments

1

Make sure your Officer class overrides the equals() (and hashCode()) method and it will be really easy:

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);

5 Comments

.equals() and hashCode()!
When you override equals, override hashCode too
@fge: is hashCode() necessary for lists?
@jlordo honestly, I don't know, but I'd never go about implementing .equals() only since you break the Object contract... And you'd have bad surprises if you decided on a Set ;)
@fge: the object contract is a good point here. I don't think hashCode() is used in this particualr use case.
0
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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.