I have two objects like following:
public class A {
private Integer id;
private String name;
private List list;
public A(Integer id
Collection merge(List list) {
return list.stream()
.collect(Collectors.toMap(a -> a.id, Function.identity(), this::merge))
.values();
}
A merge(A a1, A a2) {
if (!a1.name.equals(a2.name)) {
throw new IllegalArgumentException("We assumed same id means same name");
}
return new A(a1.id, a1.name, union(a1.list, a2.list));
}
List union(List l1, List l2) {
List result = new ArrayList<>(l1);
result.addAll(l2);
return result;
}