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.
A simple approach:
Use a classical loop, no stream operation.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments