I'd like to update items in an existing list from an incoming list.
class Person{
String id;
String name;
String age;
.
.
.
@Override
public boolean equals(Object object) {
return ... ((Person) object).id.equals(this.id);
}
}
The current list is shorter:
ArrayList<Person> currentList = Arrays.asList(
new Person("0", "A", 25),
new Person("1", "B", 35)
);
The received list is bigger, e.g.
ArrayList<Person> updatedList = Arrays.asList(
new Person("0", "X", 99),
new Person("1", "Y", 100),
new Person("2", "C", 2),
new Person("3", "D", 3),
new Person("4", "E", 5)
);
including the items(identified by their id) from current list.
I'd like to replace all the items in the current list, with the same one's from new list.
So after transformation, current list will be
{ Person(0, "X", 99), Person(1, "Y", 100) }
Is it possible to do with Stream only.
Mapand doing only one loop has a point, but for such short lists, it won’t pay off.CollectionmethodsremoveAll,retainAll, andaddAll. might be enough and might help readability. For small lists the performance would not be different, I think.