Reduce elements in a java list using Java8 streams

Coder123

I have a java list that contains elements like below:

Input List name is itemsList:

List<Items> itemsList ;

Items class:

List<String> Identifier;
List<String> websites;

Input Elements for the list:

Identifier    websites
 id1,id2      site1
 id2,id3       site3
 id5           site5
 id1           site6
 id5          site7 
 id6           site8

Result list:

   Identifier         websites
     id1,id2,id3      site1,site3,site6   
     id5                site5,site7
     id6                site8

As you can see from the result : Identifier should be grouped together if anyone Identifier is present from the other row and all websites should be combined together as well

Here is what I tried :

    itemsList.stream().reduce((v1, v2) ->
    {
        //see if identofier overlaps
        if (!Collections.disjoint(v1.getIdentifier(), (v2.getIdentifier()))) {
            v1.getIdentifier().addAll(v2.getIdentifier());
            v1.getwebsites().addAll(v2.getwebsites());
     
        } 
        return v1;
    });

my solution doesn't help much..As it only reduces the first row. I know it is not an easy one to solve.

Ole V.V.

A simple approach:

  1. Create a result list and initialize it to empty.
  2. Iterate over the input list. For each input element:
    1. Find all elements in the result list that holds either an ID or a site (or both) from the input element.
    2. Combine (reduce) them into one result list member.
    3. Also add the input element.

Use a classical loop, no stream operation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Filtering List using Java8 streams

From Dev

Java8: Reduce list of elements like sql group by

From Dev

Sum distances between points in list using Java8 Streams

From Dev

Using java8 Streams merge internal lists within a list

From Dev

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

From Dev

Java 8 Streams: Analyze same list elements

From Dev

build a collection using java8 streams

From Dev

How to convert List of Lists to single List using Java8 streams

From Dev

How to make all variations of list elements using Java streams?

From Dev

How to make all variations of list elements using Java streams?

From Dev

Java 8 streams reduce and combine List items to Map

From Dev

Using Java8 Streams to create a list of objects from another two lists

From Dev

Create a joined string of the field-values of an object-list using streams and java8

From Dev

Find total of a value from List<Obj> using Java8 streams

From Dev

Sorting by enum parameters items from a list using Java8 streams

From Dev

Find total of a value from List<Obj> using Java8 streams

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

Processing a list of maps using Java 8 streams

From Dev

How to parallel process elements in an list to sum them all using Java 8 Streams

From Dev

How to parallel process elements in an list to sum them all using Java 8 Streams

From Dev

Parsing a list using java streams

From Dev

How do I sort a List of TreeSets with java8 streams

From Dev

List Distinct elements including a count with Java Streams

From Dev

Java 8 streams: process every possible pair of elements from list

From Dev

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

From Dev

Java 8 streams: process every possible pair of elements from list

From Dev

Java 8 streams library | Grouping elements of a list and counting

From Dev

Creating a Map using Java8 streams on a nested Data Structure

From Dev

List to Map of List using java streams

Related Related

  1. 1

    Filtering List using Java8 streams

  2. 2

    Java8: Reduce list of elements like sql group by

  3. 3

    Sum distances between points in list using Java8 Streams

  4. 4

    Using java8 Streams merge internal lists within a list

  5. 5

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

  6. 6

    Java 8 Streams: Analyze same list elements

  7. 7

    build a collection using java8 streams

  8. 8

    How to convert List of Lists to single List using Java8 streams

  9. 9

    How to make all variations of list elements using Java streams?

  10. 10

    How to make all variations of list elements using Java streams?

  11. 11

    Java 8 streams reduce and combine List items to Map

  12. 12

    Using Java8 Streams to create a list of objects from another two lists

  13. 13

    Create a joined string of the field-values of an object-list using streams and java8

  14. 14

    Find total of a value from List<Obj> using Java8 streams

  15. 15

    Sorting by enum parameters items from a list using Java8 streams

  16. 16

    Find total of a value from List<Obj> using Java8 streams

  17. 17

    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

  18. 18

    Processing a list of maps using Java 8 streams

  19. 19

    How to parallel process elements in an list to sum them all using Java 8 Streams

  20. 20

    How to parallel process elements in an list to sum them all using Java 8 Streams

  21. 21

    Parsing a list using java streams

  22. 22

    How do I sort a List of TreeSets with java8 streams

  23. 23

    List Distinct elements including a count with Java Streams

  24. 24

    Java 8 streams: process every possible pair of elements from list

  25. 25

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

  26. 26

    Java 8 streams: process every possible pair of elements from list

  27. 27

    Java 8 streams library | Grouping elements of a list and counting

  28. 28

    Creating a Map using Java8 streams on a nested Data Structure

  29. 29

    List to Map of List using java streams

HotTag

Archive