Skip to content

Filter contents of list with entries matching entire contents of another list Java streams

I may be not able to search properly but this is a query in theory is simple. I have list of arrays containing numbers. I want to filter list which match all entries from another list.

Say following is my list {1,2,3},{1,2},{1},{2},{3}, {2,3}

Now If my criteria list is {1,2} I should get following result {1,2,3},{1,2}

Basically I am looking for all match for criteria list. I tried using contains in predicate but its returns like or condition

//internally it creates array 
products.add(new ProdData(344, 766));
products.add(new ProdData(344,123));
products.add(new ProdData(344,766,123));

List<Integer> matchingVolumes = new ArrayList<>();
    .filter(p -> {return matchingVolumes.contains(p);})

I want to match records # 1 and 3

products is list of ProdData objects from which chemVolume is the field I am trying to match the criteria. Order of entries does not matter.



What you seem to be looking for is:

.filter(p -> p.getAllChemVolumes().containsAll(matchingVolumes))

where you would need to define the getAllChemVolumes method within ProductData to return a Collection<Integer> based on the efficiency you seek.

User contributions licensed under: CC BY-SA
1 People found this is helpful