6

I am trying to understand Lambdas in Java 8.

Say I have a Person class that looks like this:

public class Person implements {
    String name;
    GenderEnum gender;
    int age;
    List<Person> children;
}

Now what I want to do is find all persons which are female, that have children that are younger than 10 years old.

Pre java 8 I would do it like this:

List<Person> allPersons = somePeople();
List<Person> allFemaleWithChildren = new ArrayList<>();
for(Person p : allPersons) {
    for(Person child : p.getChildren()) {
        if(child.getAge() < 10 && p.getGender() == GenderEnum.Female) {
            allFemaleWithChildren.add(p);
        }
    }
}

Now allFemaleWithChildren should have what I want. I have been trying to do the same using streams I think I need to use some sort of map, filter and reduce

allPersons.stream()
//filter females
.filter(p -> p.getGender == GenderEnum.Female)
//get the children
.map(c -> c.getChildren())
//filter the ones that are less than 10 years
.filter(c -> c.getAge() < 10)
//return a list with the result
.collect(Collectors.toList())

But this code does not compile. What am I missing.

Also, I don't understand what the reduce method can be used for.

The compiler says cannot resolve method getAge(). This is because c is apparently a collection and not the items in the collection, which is really what I want.

3
  • 1
    What does the compiler say ? And what does the Person class implement ? Commented Mar 26, 2014 at 13:37
  • The problem seems to be with the last filter method. c.getYear() because c is a collection and not iterator of the items Commented Mar 26, 2014 at 13:39
  • @Duncan done. But its not relevant in my opinion. Anyone that clearly understands this should be able to spot my mistake as I am not doing something right Commented Mar 26, 2014 at 13:43

2 Answers 2

9

At the moment (once you fix the compilation error) you would be returning a list of Children. Assuming that in your original code you meant to break as soon as you find a children under 10, the equivalent could look like:

allPersons.stream()
    //filter females
    .filter(p -> p.getGender() == GenderEnum.Female)
    //only keep females with at least one child < 10
    .filter(f -> f.getChildren().stream()
                    .anyMatch(c -> c.getAge() < 10))
    //return a list with the result
    .collect(Collectors.toList())

And indeed as commented below, you could use a few static imports, add helper methods and refactor the original code to make it more readable:

allPersons.stream()
    .filter(this::female)
    .filter(this::hasChildrenUnder10)
    .collect(toList())

//...

private boolean female(Person p) { return p.getGender() == Female; }
private boolean hasChildrenUnder10(Person parent) {
    return parent.getChildren().stream()
                    .anyMatch(c -> c.getAge() < 10));
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 I would still refactor the children filter into its own function and then use a method reference here. Perhaps something like filter(this::hasChildrenUnderTen), just to make the stream more readable in this case.
A somewhat more concise replacement for filter(pred).findAny().isPresent is anyMatch(pred).
1

You have 2 for loops, that means at some point you need another stream. Here when you call map, you map your mothers to lists of children. You then carry on as if you had a stream of children, but you have a stream of collections of children actually.

Comments

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.