Android : Compare two ArrayList of objects and find unmatching ids from the second ArrayList
You can use an inner loop, to check if the Person's id from arrayList2
corresponds to any Person id in the arrayList1
. You'll need a flag to mark if some Person was found.
ArrayList<Integer> results = new ArrayList<>();
// Loop arrayList2 items
for (Person person2 : arrayList2) {
// Loop arrayList1 items
boolean found = false;
for (Person person1 : arrayList1) {
if (person2.id == person1.id) {
found = true;
}
}
if (!found) {
results.add(person2.id);
}
}
Look at the modifications to person class
public static class Person{
//setters and getters
@Override
public boolean equals(Object other) {
if (!(other instanceof Person)) {
return false;
}
Person that = (Person) other;
// Custom equality check here.
return this.getId() == that.getId();
}
}
Have overridden equals(Object other)
then simply do this
for (Person person : arrayList1) {
arrayList2.remove(person);
}
your answer is array list 2, it will only contain odd objects