I got a list of objects from class A in a list. Some of these objects are equal in id and name but not in list <B> , and list b is ALWAYS different.
I need to merge these so that my list is only made out of object a's with same name and id exists and all the b from same group are collected I can make use of jdk 8 plus utils so streams are ok to use here.. Although I think reflection here is more usable?
PS: I can not change content of a of b class as they are generated classes and no access / expansion possibility
@Test public void test() { List.of(new A(1, "a1", List.of(new B(1, "1b"))), new A(1, "a1", List.of(new B(2, "2b"))), new A(2, "a2", List.of(new B(3, "3b")))); //expected List.of(new A(1, "a1", List.of(new B(1, "1b"), new B(2, "2b"))), new A(2, "a2", List.of(new B(3, "3b")))); } class A { public A(int id, String name, List<B> listB) { this.id = id; this.name = name; this.listB = listB; } int id; String name; List<B> listB; } class B { public B(int id, String name) { this.id = id; this.name = name; } int id; String name; }