Java - Filter a list with streams based on ids from another list
I have a list of
Product objects with 30 objects.
I have created a fix list with strings of ids.
List<String> productsIdsForFreeSchoolYear = Arrays.asList("169", "172", "198", "213", "358", "4529", "6602", "5958");
What I want to do is to get list of
Product only contains the ids from
This is what i tried, but its seems its only check if the ids exists? Im i right?
productsSelectable.stream() .distinct() .filter(productsIdsForFreeSchoolYear::contains) .collect(Collectors.toList());
productsSelectable is the list of
There are 1 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Java 8, Java Stream. These have been categorized in sections for a clear and precise explanation.