I have 2 lists:
List1<Object1>:Object1 (name1, id1)List2<Object2>:Object2 (name2, id2)
The size of list1 is the same as list2.
I want to iterate over list2 and if the name2 of list2 is not null, then update the name1 of list1.
Jere is the code using old Java:
for(Object1 obj1:list1) {
for(Object2 obj2:list2) {
if(obj1.getId1.equals(obj2.getId2)) {
obj1.setName1(obj2.getName2);
}
}
}
Which is the best way to implement this with java.util.stream?
idmean a unique identifier for your objects? In that case a map with the id as a key is more appropriate than a list. It would also reduce time complexity to O(n) (in case of aHashMap) instead of O(n^2).