0

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.

10
  • I don't understand your logic. Is Person.isFemale a static method? What are you testing when you call Person.isFemale(n).test(e)? What does Person.isFemale(n) return? Commented Aug 27, 2014 at 14:40
  • the filter-method accepts a Predicate which is used here. Commented Aug 27, 2014 at 14:43
  • This comment doesn't answer my question. I'm trying to understand your Java7 code in order to figure out how it should look in Java8. Commented Aug 27, 2014 at 14:44
  • getFilteredRes_OLD() uses java8-Predicate, but not stream/filter. both methods compiles to java8. But I want to change the _OLD() to _NEW() having the same result. Is there a way to put a list into the all.stream().filter()-method? Commented Aug 27, 2014 at 14:48
  • Please clarify. In the first code snippet, you call isFemale(n). But the isFemale() 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)? Commented Aug 27, 2014 at 14:54

1 Answer 1

3

Here's, I think, the filter instruction you want:

.filter(person -> names.stream().anyMatch(
                      name -> Person.isFemale(name).test(person)))

For each person, it tests if there is at least one name N in the list of names for which the Person.isFemale(N).test(P) returns true.

It would be much simpler, and create less instances of Predicate, if you simply had a method

public boolean isFemale(Person p, String name) {
    return p.getFirstName() != null && p.getFirstName().equalsIgnoreCase(test);
}

The code would become

.filter(person -> names.stream().anyMatch(name -> isFemale(person, name)))

You should also rename isFemale(), because it doesn't test at all if the person is female. It tests if a person has a given first name (ignoring the case).

Sign up to request clarification or add additional context in comments.

1 Comment

You should probably turn names into a set of all lower case names and just do p -> nameSet.contains (p.flgetFirstName ()) instead of scanning the names list each time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.