I have a class Test
public class Test{
String codes;
String field 1;
....
String field n;
}
I have a list of test objects
List<Test> objects, code can be one or more with a comma separated
testObj1("A", "field1".."fieldn")
testObj2("B,C", ...)
testObj3("D,E,F", ....)
testObj4("G", ...)
Trying to convert this list1 to new list2 with each code A, B, C... to its own object by retaining the remaining fields.
List<Test>
testObj1("A", ....)
testObj2("B", ....)
testObj3("C", ....)
list1.stream().collect(Collectors.toList())
I achieved this using loops (Sudo code) but looking for better logic
for(loop thru list1){
String[] codesArr = testObj1.codes.split(",");
for (String code : codesArr) {
//Create new Obj
Test obj = new Test(code, testObj1.copyotherfields);
//Add obj to list2
}
}
You can use
Stream.mapwithflatMapas :This assumes that your
Testclass would have a constructor similar to the following implementation: