Java8: Reduce list of elements like sql group by

aweibell

After a stream().filter().map() on a List I have a data structure of type List<List<Object>> that looks like this:

[["1","a",20],
 ["1","b",10],
 ["2","a",10],
 ["2","b",30]]

What I want is to group by the value of the first element of the inner list, leave the middle element out and finally sum the last elements for each "group".

[["1", 30],
 ["2", 40]]

Sorry if this is obvious for some of you, but I have yet to find any example of how to achieve this. I assumed it could be done by Stream.reduce(U identity, BiFunction accumulator, BinaryOperator combiner) but so far I haven't succeeded. If someone could provide some example code for this, I believe it would be appreciated by many others too.

wassgren

The following code may be of help:

List<List<Object>> originalList = Arrays.asList(
        Arrays.asList("1", "a", 20),
        Arrays.asList("1", "b", 10),
        Arrays.asList("2", "a", 10),
        Arrays.asList("2", "b", 30)
);

final Map<Object, Integer> collectedMap =
        originalList.stream()
                .collect(Collectors.groupingBy(
                        e -> e.get(0),
                        Collectors.summingInt(e -> (Integer) e.get(2))));

System.out.println(collectedMap);

The output is:

{1=30, 2=40}

Basically, what the code does is to group by the first value in the sublist (get(0)). Then it sums the integers by the use of summingInt. However, it groups the entire thing in a Map - if some other collection is required the stream must be slightly changed.

E.g. to collect the whole thing as a List:

final List<List<Object>> collectedList =
    collectedMap.entrySet()
             .stream()
             .map(e -> Arrays.asList(e.getKey(), e.getValue()))
             .collect(Collectors.toList());

System.out.println(collectedList);

Then, the output will be:

[[1, 30], [2, 40]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Reduce elements in a java list using Java8 streams

From Dev

Java8 group a list of lists to map

From Dev

Java: Group list elements by type

From Dev

How to group elements of a List by elements of another in Java 8

From Dev

Group and Reduce list of objects

From Dev

Java8 Stream API: Group a list into a custom class

From Java

How to add elements of a Java8 stream into an existing List

From Dev

java8 make immutable List after removing null elements

From Dev

Group by and sum objects like in SQL with Java lambdas?

From Dev

SQL-like MAX and GROUP BY over a list of dicts

From Dev

Group elements in list by key

From Dev

Group elements in list by key

From Dev

Java 8 reduce list to linkedlist

From Dev

Java 8 reduce list to linkedlist

From Dev

Java 8/JDK8 Stream's functions Map/Reduce To group List<String> into Map<String, List<String>>

From Dev

Group list by elements of nested list

From Dev

How to group List<Map<String,Object>> to Map<String,List<Map<String,Object>> in Java8

From Dev

How to access elements in a java List like Python negative slicing?

From Dev

How to access elements in a java List like Python negative slicing?

From Dev

Using Java Streams to group together a List of objects by an attribute and reduce them to a new list of object with the average of another attribute

From Dev

Group list elements into tuples by index

From Dev

Group and Merge elements in same list

From Dev

Group sorting JQuery list elements

From Dev

Counting like elements in a list and appending list

From Dev

java8 reduce with useless combiner

From Java

Reduce the size of list with Java 8 stream

From Dev

Reduce the size of list with Java 8 stream

From Dev

SQL - Group by Elements of Comma Delineation

From Dev

group list elements based on another list

Related Related

  1. 1

    Reduce elements in a java list using Java8 streams

  2. 2

    Java8 group a list of lists to map

  3. 3

    Java: Group list elements by type

  4. 4

    How to group elements of a List by elements of another in Java 8

  5. 5

    Group and Reduce list of objects

  6. 6

    Java8 Stream API: Group a list into a custom class

  7. 7

    How to add elements of a Java8 stream into an existing List

  8. 8

    java8 make immutable List after removing null elements

  9. 9

    Group by and sum objects like in SQL with Java lambdas?

  10. 10

    SQL-like MAX and GROUP BY over a list of dicts

  11. 11

    Group elements in list by key

  12. 12

    Group elements in list by key

  13. 13

    Java 8 reduce list to linkedlist

  14. 14

    Java 8 reduce list to linkedlist

  15. 15

    Java 8/JDK8 Stream's functions Map/Reduce To group List<String> into Map<String, List<String>>

  16. 16

    Group list by elements of nested list

  17. 17

    How to group List<Map<String,Object>> to Map<String,List<Map<String,Object>> in Java8

  18. 18

    How to access elements in a java List like Python negative slicing?

  19. 19

    How to access elements in a java List like Python negative slicing?

  20. 20

    Using Java Streams to group together a List of objects by an attribute and reduce them to a new list of object with the average of another attribute

  21. 21

    Group list elements into tuples by index

  22. 22

    Group and Merge elements in same list

  23. 23

    Group sorting JQuery list elements

  24. 24

    Counting like elements in a list and appending list

  25. 25

    java8 reduce with useless combiner

  26. 26

    Reduce the size of list with Java 8 stream

  27. 27

    Reduce the size of list with Java 8 stream

  28. 28

    SQL - Group by Elements of Comma Delineation

  29. 29

    group list elements based on another list

HotTag

Archive