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

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

Creating a Map using Java8 streams on a nested Data Structure

From Dev

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

From Dev

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

From Dev

Sum distances between points in list using Java8 Streams

From Dev

Java 8 Streams: Analyze same list elements

From Dev

Parsing a list using java streams

From Dev

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

From Dev

build a collection using java8 streams

From Dev

Java 8 streams reduce and combine List items to Map

From Dev

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

From Dev

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

From Dev

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

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

List Distinct elements including a count with Java Streams

From Dev

List to Map of List using java streams

From Dev

Sorting by enum parameters items from a list using Java8 streams

From Dev

How do I sort a List of TreeSets with java8 streams

From Dev

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

From Dev

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

From Dev

Processing a list of maps using Java 8 streams

From Dev

Filtering List using Java8 streams

From Dev

Using java8 Streams merge internal lists within a list

From Dev

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

From Dev

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

From Dev

Java8: Reduce list of elements like sql group by

From Dev

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

From Dev

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

Related Related

  1. 1

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

  2. 2

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

  3. 3

    Creating a Map using Java8 streams on a nested Data Structure

  4. 4

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

  5. 5

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

  6. 6

    Sum distances between points in list using Java8 Streams

  7. 7

    Java 8 Streams: Analyze same list elements

  8. 8

    Parsing a list using java streams

  9. 9

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

  10. 10

    build a collection using java8 streams

  11. 11

    Java 8 streams reduce and combine List items to Map

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    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

  16. 16

    List Distinct elements including a count with Java Streams

  17. 17

    List to Map of List using java streams

  18. 18

    Sorting by enum parameters items from a list using Java8 streams

  19. 19

    How do I sort a List of TreeSets with java8 streams

  20. 20

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

  21. 21

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

  22. 22

    Processing a list of maps using Java 8 streams

  23. 23

    Filtering List using Java8 streams

  24. 24

    Using java8 Streams merge internal lists within a list

  25. 25

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

  26. 26

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

  27. 27

    Java8: Reduce list of elements like sql group by

  28. 28

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

  29. 29

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

HotTag

Archive