From the following input
[
[{group=a, value=cat}, {group=b, value=dog}],
[{group=a, value=cow}, {group=b, value=bat}]
]
how do I get the following output
{
a=[{value=cat}, {value=cow}],
b=[{value=dog}, {value=bat}]
}
using Java 8 streams?
I have the following standard solution
Map<String, String > map1 = new LinkedHashMap<>();
map1.put("group", "a");
map1.put("value", "cat");
Map<String, String > map2 = new LinkedHashMap<>();
map2.put("group", "b");
map2.put("value", "dog");
Map<String, String > map3 = new LinkedHashMap<>();
map3.put("group", "a");
map3.put("value", "cow");
Map<String, String > map4 = new LinkedHashMap<>();
map4.put("group", "b");
map4.put("value", "bat");
List<Map<String, String>> list1 = Arrays.asList(map1, map2);
List<Map<String, String>> list2 = Arrays.asList(map3, map4);
List<List<Map<String, String>>> input = Arrays.asList(list1, list2);
Map<String, List<Map<String, String>>> output = new LinkedHashMap<>();
for (List<Map<String, String>> list : input) {
for (Map<String, String> map : list) {
String group = map.get("group");
if (!output.containsKey(group)) {
output.put(group, new ArrayList<>());
}
List<Map<String, String>> values = output.get(group);
values.add(map);
}
}
I saw that there is a Collectors.groupingBy
method, but I couldn't figure out how to use it.
Map<String, List<Map<String, String>>> output = input.stream()
.<am I missing out some steps here?>
.collect(Collectors.groupingBy(<what goes here?>))
To generate the same output you should flatten list of lists to the single list via flatMap
before grouping:
output = input.stream().flatMap(List::stream)
.collect(Collectors.groupingBy(map -> map.get("group")));
This code generates the same output like the imperative code you've posted:
{
a=[{group=a, value=cat}, {group=a, value=cow}],
b=[{group=b, value=dog}, {group=b, value=bat}]
}
Note however that it differs from your desired output. To get the desired output you may need to specify downstream collector:
output = input.stream()
.flatMap(List::stream)
.collect(Collectors.groupingBy(map -> map.get("group"),
Collectors.mapping(
map -> Collections.singletonMap("value", map.get("value")),
Collectors.toList())));
Now the result is
{
a=[{value=cat}, {value=cow}],
b=[{value=dog}, {value=bat}]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With