In my java
code I have a structure Person
:
public class Person {
String name;
String distance;
String address;
String field1;
String field2;
}
Now, I have an ArrayList<Person> people
that contains couple objects. I also have another ArrayList<Person> otherPeople
that contains other objects.
I want to produce a 3rd list that contains all objects from people
that are not already in otherPeople
.
But I need to compare the objects only by their name
, distance
and address
, I don't care of values of field1
and field2
.
I thought about creating 2 for loops:
for (Person newPerson: people) {
for (Person oldPerson: otherPeople) {
if(newPerson.getName().equals(oldPerson.getName()) &&
newPerson.getDistance().equals(oldPerson.getDistance()) &&
newPerson.getAddress().equals(oldPerson.getAddress()) {
but I don't know how to proceed, especially since I cannot remove elements from the list I'm iterating through... Can you help me with that?