试图理解Java 8中的lambda和stream

She*_*ari 6 java collections lambda java-8 java-stream

我试图在Java 8中理解Lambdas.

假设我有一个如下所示的Person类:

public class Person implements {
    String name;
    GenderEnum gender;
    int age;
    List<Person> children;
}
Run Code Online (Sandbox Code Playgroud)

现在我要做的是找到所有女性,有10岁以下孩子的人.

前java 8我会这样做:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在allFemaleWithChildren应该拥有我想要的东西.我一直试图使用流做同样的事情,我认为我需要使用某种地图,过滤和减少

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())
Run Code Online (Sandbox Code Playgroud)

但是这段代码没有编译.我错过了什么

另外,我不明白reduce方法可以用于什么.

编译说 cannot resolve method getAge().这是因为c它显然是一个集合而不是集合中的项目,这正是我想要的.

ass*_*ias 9

此刻(一旦你修复了编译错误),你将返回一个Children列表.假设在您的原始代码中,一旦您找到10岁以下的孩子,您打算打破,相当于:

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())
Run Code Online (Sandbox Code Playgroud)

确实如下所述,您可以使用一些静态导入,添加辅助方法并重构原始代码以使其更具可读性:

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));
}
Run Code Online (Sandbox Code Playgroud)