I am currently working with a List<Map<String, Object>>
where I am trying to group across various keys within the map.
This seems to work nicely using the Java 8 Stream
s:
Map<Object, Map<Object, List<Map<String, Object>>>> collect =
list
.stream()
.collect(Collectors.groupingBy(
item -> item.get("key1"),
Collectors.groupingBy(item -> item.get("key2"))
));
As expected this give me a Map<Object, Map<Object, List<Map<String, Object>>>>
which works well where the possible grouped results are greater than 1.
I have various examples where the grouping being done will always result in a single item in the lowest level list, for example.
List of Rows
{
[reference="PersonX", firstname="Person", dob="test", lastname="x"],
[reference="JohnBartlett", firstname="John", dob="test", lastname="Bartlett"]
}
Grouped by reference
Currently - grouped into a list with 1 Map<String,Object>
[PersonX, { [reference="PersonX", firstname="Person", dob="test", lastname="x"]}],
[JohnBartlett, { [reference="JohnBartlett", firstname="John", dob="test", lastname="Bartlett"]}]
Preference - No List just a single Map<String,Object>
[PersonX, [reference="PersonX", firstname="Person", dob="test", lastname="x"]],
[JohnBartlett, [reference="JohnBartlett", firstname="John", dob="test", lastname="Bartlett"]]
is there a way within the streams to force the output for these instances to be Map<Object, Map<Object, Map<String, Object>>>
- so a single Map<String,Object>
rather than a List
of them?
Any help would be greatly appreciated.