So I have this code that "works" (replacing some names for simplicity):
Map<String, Map<String, ImmutableList<SomeClassA>>> someMap =
someListOfClassA.stream()
.filter(...)
.collect(Collectors.groupingBy(SomeClassA::someCriteriaA,
Collectors.groupingBy(SomeClassA::someCriteriaB, GuavaCollectors.toImmutableList()
)
));
However, I want to change this code so that the inner collection is of SomeClassB after grouping by SomeClassA fields. For example, if the classes look like this:
assuming they both have all args constructors
class SomeClassA {
String someCriteriaA;
String someCriteriaB;
T someData;
String someId;
}
class SomeClassB {
T someData;
String someId;
}
And there's a method somewhere:
public static Collection<SomeClassB> getSomeClassBsFromSomeClassA(SomeClassA someA) {
List<Some List of Class B> listOfB = someMethod(someA);
return listOfB; // calls something using someClassA, gets a list of SomeClassB
}
I want to flatten the the resulting lists of SomeClass Bs into
Map<String, Map<String, ImmutableList<SomeClassB>>> someMap =
someListOfClassA.stream()
.filter(...)
. // not sure how to group by SomeClassA fields but result in a list of SomeClassB since one SomeClassA can result in multiple SomeClassB
I'm not sure how this would fit into the code above. How can I collect a bunch of lists based on SomeClassB into a single list for all the values of SomeClassA? If a single ClassA maps to a single ClassB I know how to get it to work using Collectors.mapping but since each ClassA results in multiple ClassBs I'm not sure how to get it to work.
Any ideas would be appreciated. Thanks!