Here's my domain classes Location
and Address
:
public class Location {
GPS gps;
Address address;
// etc.
}
public class Address {
String street;
String number;
// etc.
}
Let's say I have a List of String
's
List<String> houseNumbers
and a List of Locations
List<Location> locations
Now, I want to have only those Location
s where the Location.address.number
matches with any of the String
s in the houseNumbers
list.
I've the following, which produces a compilation error: "expecting a
Predicate <? super java.lang.String>
instead of a String".
My attempt:
List<Location> filteredLocations = locations.stream()
.anyMatch(location ->
housenumbers.stream().anyMatch(location.address.number)
);
But how do I make sure to compare to every item in the houseNumbers
List?
The following snippet did the trick:
List<Location> filteredLocations =
locations.stream().filter(
location -> housenumbers.contains(location.address.number)
).collect(Collectors.toList());