locations as a parameter. I am populating a list of use...">

Java 8 java expression to find "contains"

I have a method that takes List<String> locations

as a parameter. I am populating a list of users with the same method based on a specific condition. Now I want the user to be present in these locations. In short, a user is valid if the user's location is present in the list of provided locations. places.

This is my current code:

primaryList.stream()
        .filter(some_pattern_matching)[.MATCH THE LOCATION HERE]
        .map(user -> user.getNames())
        .collect(toList())

      

is there a way to say locations.contains(user -> user.getLocation())

OR user -> user.getLocation().isPresentIn(locations)

OR have locations converted to one stream and then do the mapping?

This is my current code:

 .filter(locations.stream().filter(loc -> loc.equalsIgnoreCase(s.getLocation())))

      

which of course doesn't compile.

+3


source to share


1 answer


You mean this:



.filter(user -> locations.contains(user.getLocation())

      

+4


source







All Articles