Reputation: 7737
Given a class Item:
public class Item {
private String field1;
private String field2;
private String field3;
private Integer field4;
// getters, constructor...
}
And another class Group (field1 and field2 store the equivalent fields from Item):
public class Group {
private String field1;
private String field2;
}
I have a List<Item>
which I need to aggregate into a map of the following structure:
Map<Group, Map<Field3, List<Field4>>>
Example data:
Field1 | Field2 | Field3 | Field4
------ | ------ | ------ | ------
"f1" | "f2" | "a" | 1
"f1" | "f2" | "a" | 2
"f1" | "f2" | "a" | 3
"f1" | "f2" | "b" | 4
"f1" | "f2" | "b" | 5
"f1" | "f2" | "c" | 6
"f1a" | "f2a" | "a" | 7
"f1a" | "f2a" | "a" | 8
The expected result would look like:
Group(field1=f1a, field2=f2a)={b=[7, 8]}, Group(field1=f1, field2=f2)={a=[1, 2, 3], b=[4, 5], c=[6]}
So far I have been able to aggregate by Field1, Field2 and Field3, such that I have the following structure (where GroupEx
represents a POJO that holds Field1, Field2 and Field3):
Map<GroupEx, List<Field4>>
The code to aggregate in this way is:
Map<GroupEx, List<Integer>> aggregated = items.stream()
.collect(Collectors.groupingBy(item -> new GroupEx(x.getField1(), x.getField2(), x.getField3())
, Collectors.mapping(Item::getField4, Collectors.toList())));
I'm struggling to get the syntax correct to allow me to group by Field1 and Field2, and to then group by Field3 and Field4 into a map in the way I need.
The "long hand" syntax is:
Map<Group<String, String>, Map<String, List<Integer>>> aggregated = new HashMap<>();
for (Item item : items) {
Group key = new Group(item.getField1(), item.getField2());
Map<String, List<Integer>> field3Map = aggregated.get(key);
if (field3Map == null) {
field3Map = new HashMap<>();
aggregated.put(key, field3Map);
}
List<Integer> field4s = field3Map.get(item.getField3());
if (field4s == null) {
field4s = new ArrayList<>();
field3Map.put(item.getField3(), field4s);
}
field4s.add(item.getField4());
}
Is someone able to show me how my target grouping can be achieved?
Upvotes: 3
Views: 1306
Reputation: 93842
This is where the downstream collectors feature comes handy.
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
...
List<Item> list = ....
Map<Group, Map<String, List<Integer>>> map =
list.stream().collect(groupingBy(i -> new Group(i.getField1(), i.getField2()),
groupingBy(Item::getField3, mapping(Item::getField4, toList()))));
First, you group the items by their Group
field (you a Map<Group, List<Item>>
at this point), then you map each value (List<Item>
) to a map again where you group by the field3 (Map<Group, Map<Field3, List<Item>>
).
Then you map the values in the second map by the field4 and you collect them into a list, to finally get a Map<Group, Map<Field3, List<Field4>>
.
Given your input, it outputs:
{Group{field1='f1a', field2='f2a'}={a=[7, 8]}, Group{field1='f1', field2='f2'}={a=[1, 2, 3], b=[4, 5], c=[6]}}
Upvotes: 8