Using Java 8 streams groupingBy on a list of list of maps?

Kohányi Róbert

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?>))
Tagir Valeev

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}]
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Processing a list of maps using Java 8 streams

From Dev

GroupingBy using Java 8 streams

From Dev

Java 8 streams - merging a list of Maps

From Dev

Java 8 Streams - Nested Maps to List

From Dev

Filtering List using Java8 streams

From Dev

Java 8 streams/maps/filters to modify or delete list elements on the fly

From Dev

groupingBy & Java 8 - after groupingBy convert map to a list of objects

From Dev

Mapping a list to Map Java 8 stream and groupingBy

From Dev

Java 8 Streams groupingBy collector

From Dev

Returning default list if the list is empty using java 8 Streams?

From Dev

iterate List inside list using java 8 streams

From Java

Reduce elements in a java list using Java8 streams

From Java

Is there a way to coalesce repeated numbers in a list using streams in Java 8?

From Java

Hash Map object list iteration using Java 8 Streams

From Java

Iterating a list inside a map of map using java 8 streams

From Dev

Sum distances between points in list using Java8 Streams

From Dev

Getting only required objects from a list using Java 8 Streams

From Dev

How to map elements of the list to their indices using Java 8 streams?

From Dev

How to multiply values in a list using java 8 streams

From Dev

Modify property value of the objects in list using Java 8 streams

From Dev

Is there a way to coalesce repeated numbers in a list using streams in Java 8?

From Dev

Iterating a list inside a map of map using java 8 streams

From Dev

Create a map of String and sorted list using Java 8 streams API

From Dev

Filter and modify list object using java 8 streams

From Dev

How to separate a List by a condition using Java 8 streams

From Dev

List only folders of certain depth using Java 8 streams

From Dev

Creating a 2 dimensional list using java 8 streams

From Dev

How to multiply values in a list using java 8 streams

From Dev

Using java8 Streams merge internal lists within a list

Related Related

  1. 1

    Processing a list of maps using Java 8 streams

  2. 2

    GroupingBy using Java 8 streams

  3. 3

    Java 8 streams - merging a list of Maps

  4. 4

    Java 8 Streams - Nested Maps to List

  5. 5

    Filtering List using Java8 streams

  6. 6

    Java 8 streams/maps/filters to modify or delete list elements on the fly

  7. 7

    groupingBy & Java 8 - after groupingBy convert map to a list of objects

  8. 8

    Mapping a list to Map Java 8 stream and groupingBy

  9. 9

    Java 8 Streams groupingBy collector

  10. 10

    Returning default list if the list is empty using java 8 Streams?

  11. 11

    iterate List inside list using java 8 streams

  12. 12

    Reduce elements in a java list using Java8 streams

  13. 13

    Is there a way to coalesce repeated numbers in a list using streams in Java 8?

  14. 14

    Hash Map object list iteration using Java 8 Streams

  15. 15

    Iterating a list inside a map of map using java 8 streams

  16. 16

    Sum distances between points in list using Java8 Streams

  17. 17

    Getting only required objects from a list using Java 8 Streams

  18. 18

    How to map elements of the list to their indices using Java 8 streams?

  19. 19

    How to multiply values in a list using java 8 streams

  20. 20

    Modify property value of the objects in list using Java 8 streams

  21. 21

    Is there a way to coalesce repeated numbers in a list using streams in Java 8?

  22. 22

    Iterating a list inside a map of map using java 8 streams

  23. 23

    Create a map of String and sorted list using Java 8 streams API

  24. 24

    Filter and modify list object using java 8 streams

  25. 25

    How to separate a List by a condition using Java 8 streams

  26. 26

    List only folders of certain depth using Java 8 streams

  27. 27

    Creating a 2 dimensional list using java 8 streams

  28. 28

    How to multiply values in a list using java 8 streams

  29. 29

    Using java8 Streams merge internal lists within a list

HotTag

Archive