My goal is to expand List<A>, where each A has an attribute List<B> to a List<A>, where each A will contain a list with a single distinct object B.
Considering A and B have this structure:
class A{
String idA;
List<B> listOfB;
}
class B{
Long idB;
String name;
}
I'd like to expand the List<A> so that it contains an object A for each distinct contained object B (where two objects B are differentiated by the attribute name).
For example
[
{
"idA": 1,
"listOfB": [
{
"idB": 3,
"name": "Foo"
}
]
},
{
"idA": 2,
"listOfB": [
{
"idB": 3,
"name": "Foo"
},
{
"idB": 4,
"name": "Bar"
}
]
}
]
should be transformed to
{
"idA": 1,
"listOfB": [
{
"idB": 3,
"name": "Foo"
}
]
},
{
"idA": 2,
"listOfB": [
{
"idB": 3,
"name": "Foo"
}
]
},
{
"idA": 2,
"listOfB": [
{
"idB": 4,
"name": "Bar"
}
]
}
]
How can I do this using streams?