Before starting, I think this question has a very simple answer, which I simply ignore. I thought that a few more eyes on the question at hand would be able to point out my problem pretty quickly.
I have two lists of ArrayLists that I want to compare and remove duplicates from each of them. The first ArrayList is the ArrayList older information, where the second ArrayList contains the new information.
In this way
ArrayList<Person> contactList = new ArrayList(); contactList.add(new Person("Bob"); contactList.add(new Person("Jake"); contactList.add(new Person("Joe"); ontactList.add(new Person("Rob"); ArrayList<Person> updatedContactList = new ArrayList(); updatedContactList.add(new Person("Bob"); updatedContactList.add(new Person("Jake"); updatedContactList.add(new Person("Joe"); updatedContactList.add(new Person("Phil");
My Person class is very simple, created just for this example.
public class Person { private String name; public Person(String a_name) { name = a_name; } public String getName() { return name; } }
So, using the examples above, I want to remove all duplicates. I try to save it in only two ArrayLists, if possible, but I am ready to make a deep clone of one of the ArrayLists, if I need to.
So, I want the resulting ArrayList have the following information in it after comparison
contactList //removed Person - Rob updatedContactList //new Person - Phil
Here is the code I compiled
for(int i = 0; i < contactList.size(); i++) { for(int j = 0; j < updatedContactList.size(); j++) { if(contactList.get(i).getName().equals(updatedContactList.get(j).getName())) {
I can remove Person from one of the ArrayLists in the above loop, otherwise I get the wrong results.
So my question is, is there an easy way to remove duplicate elements from both ArrayLists? If so, how do I do this.
I understand that I could probably deeply clone the updated ArrayList and just remove objects from this, but I wonder if there is a way without cloning it.
I also understand that I could just fill all the elements in Set and it will remove duplicates, but I want individual Person objects to be deleted.