Вопрос

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.

Это было полезно?

Решение

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));
}

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top