How could I do the following with Java Streams?
Let's say I have the following classes:
class Foo {
Bar b;
}
class Bar {
String id;
String date;
}
I have a List<Foo>
and I want to convert it to a Map <Foo.b.id, Map<Foo.b.date, Foo>
. I.e: group first by the Foo.b.id
and then by Foo.b.date
.
I'm struggling with the following 2-step approach, but the second one doesn't even compile:
Map<String, List<Foo>> groupById =
myList
.stream()
.collect(
Collectors.groupingBy(
foo -> foo.getBar().getId()
)
);
Map<String, Map<String, Foo>> output = groupById.entrySet()
.stream()
.map(
entry -> entry.getKey(),
entry -> entry.getValue()
.stream()
.collect(
Collectors.groupingBy(
bar -> bar.getDate()
)
)
);
Thanks in advance.