I have a database table with Person objects. My web service receives a list of Person objects to update this Person table. My application now contains 2 lists:
List<Person> current;
List<Person> updated;
I want to iterate through these lists and create a new list that I will use to update the Person table.
Person {
String fName;
String lName;
String age;
String email;
}
fName and lName are used to identify existing records. The following is an example.
Current
Joe, Bloggs, 18, joe@me.com
Jane, Bloggs, 21, jane@me.com
Flo, Bloggs, 25, flo@me.com
New
Joe, Bloggs, 18, joe_bloggs@me.com
Jane, Bloggs, 21, jane@me.com
Flo, Bloggs, 90, flo@me.com
Records to update database with
Joe, Bloggs, 18, joe_bloggs@me.com
Flo, Bloggs, 90, flo@me.com
private newList(List<Person> current, List<Person> new) {
List<Person> toUpdate = new ArrayList();
for(Person person : new) {
Person found = current.stream().filter(p->p.equals(person)).findFirst().orElse(person);
if (found.age!=person.age OR found.email!=person.email) {
toUpdate.add(person);
}
return toUpdate;
}