Suppose I have two list which i want to get merged into one
Merge Condition : compare with id
Case 1 :
List 1 = [{id=1,name=null},{id=2,name=null},{id=3,name=c}]
List 2 = [{id=1,name=a},{id=2,name=b}]
After the merge, it should be [{id=1,name=a},{id=2,name=b},{id=3,name=c}]
Case 1 :
List 1 = [{id=1,name=null}]
List 2 = [{id=1,name=a},{id=2,name=b}]
After the merge, it should be [{id=1,name=a},{id=2,name=b}]
My code:
for (Object ObjofList1: list1) {
List<DummyObject> do = new ArrayLIst();
SomeObject sm = list2.stream()
.filter(list2object -> list2object.getId().equals(ObjofList1.getId()))
.findAny()
.orElse(ObjofList1);
do.add(dealerSalesTempObject);
}
It is working fine if list2
has more elements in it, but if list1
has more element then we won't be able to merge because of less iteration.
My question is i don't want to see which one is shorter or longer but at last all objects should be in one list..