I’m trying to convert a List into a Map of Objects where that Objects holds the List.
Consider the following classes:
class A {
String code;
}
class B {
A a;
String value;
}
AB {
A a;
List<B> bList;
}
I have a List<B> sourceList
that I want to convert into a Map<String, AB> targetMap
If we have the following data for sourceList:
[
{
value = "foo",
a = {code = "code1"}
},
{
value = "bar"
a = {code = "code1"}
{
value = "hello"
a = {code = "code2"}
}
]
The targetMap should look like this:
{
"code1" =
{
a = {code = "code1"}
bList =
[
{
value = "foo",
a = {code = "code1"}
},
{
value = "bar",
a = {code = "code1"}
}
]
},
"code2" =
{
a = {code = "code2"}
bList =
[
{
value = "hello"
a = {code = "code2"}
}
]
}
}
How can I do this?
I tried using the Collectors.groupingBy
and Collectors.mapping
but both looks like only results in a Map of a Collection.