I have something like this:
// common code without java8-closures/stream-api (this works):
public static List<Person> getFilteredRes_OLD(List<Person>all, List<String> names){
List<Person> pos = new ArrayList<>();
for(Person e: all){
for(String n: names){
if(Person.isFemale(n).test(e)){
pos.add(e);
}
}
}
return pos;
}
public static Predicate<Person> isFemale(String test) {
return p -> p.getFirstName()!=null && p.getFirstName().equalsIgnoreCase(test);
}
Now, I want to use the new Stream-filter API in Java 8:
// JAVA 8 code (this works, but only for a specified filter-value):
public static List<Person> getFilteredRes_NEW(List<Person>all, List<String> names){
return all.stream()
// how can I put the list 'names" into the filter-Method
// I do not want to test only for 'Lisa' but for the wohle "names"-list
.filter( Person.isFemale("Lisa") )
.collect(Collectors.<Person>toList());
return pos;
}
My Question for getFilteredRes_NEW() is:
How can I put the list of 'names" into the filter-Method? I do not want to test only for 'Lisa' but for the wohle "names"-list within the stream.
Person.isFemalea static method? What are you testing when you callPerson.isFemale(n).test(e)? What doesPerson.isFemale(n)return?isFemale(n). But theisFemale()method posted right after doesn't take any argument. So that doesn't compile. Also, do you want to add the same person for each name in the list for which Person.isFemale(n).test(e) is true (which is what your old method does)?