简体   繁体   中英

stream().filter between Classes

I have 2 classes Mother and Newborn Class Mother:

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Mother extends NewBorn {


    private List<NewBorn> newBornList = new ArrayList<>();
    private Set<NewBorn> children;

    private int id;
    private String name;
    private int age;

    public Mother(Mother mother, List<NewBorn> newBornList, Set<NewBorn> children, int id, String name, int age) {
        super(mother);
        this.newBornList = newBornList;
        this.children = children;
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Mother(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Mother(int id, String gender, String name, int birthdate, int weight, int height, List<NewBorn> newBornList, Set<NewBorn> children, int id1, String name1, int age) {
        super(id, gender, name, birthdate, weight, height);
        this.newBornList = newBornList;
        this.children = children;
        this.id = id1;
        this.name = name1;
        this.age = age;
    }

    public List<NewBorn> getNewBornList() {
        return newBornList;
    }

    public void setNewBornList(List<NewBorn> newBornList) {
        this.newBornList = newBornList;
    }

    public Set<NewBorn> getChildren() {
        return children;
    }

    public void setChildren(Set<NewBorn> children) {
        this.children = children;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Mother{" +
                "newBornList=" + newBornList +
                ", children=" + children +
                ", id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Class NewBorn:

public class NewBorn {
    private Mother mother;

    public NewBorn(Mother mother) {
        this.mother = mother;
    }
    public NewBorn() {

    }
    public Mother getMother() {
        return mother;
    }
    public void setMother(Mother mother) {
        this.mother = mother;
    }
    private int id;
    private String gender;
    private String name;
    private int birthdate;
    private int weight;
    private int height;
    private int motherId;

    public NewBorn(int id, String gender, String name, int birthdate, int weight, int height) {
        this.id = id;
        this.gender = gender;
        this.name = name;
        this.birthdate = birthdate;
        this.weight = weight;
        this.height = height;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(int birthdate) {
        this.birthdate = birthdate;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getMotherId() {
        return motherId;
    }

    public void setMotherId(int motherId) {
        this.motherId = motherId;
    }

    @Override
    public String toString() {
        return "NewBorn{" +
                "mother=" + mother +
                ", id=" + id +
                ", gender='" + gender + '\'' +
                ", name='" + name + '\'' +
                ", birthdate=" + birthdate +
                ", weight=" + weight +
                ", height=" + height +
                ", motherId=" + motherId +
                '}';
    }

}

I have to get the mother older then 25years old that have child more then 4000g weigth

I did the following

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) throws IOException {

        List<Mother> motherabove = above25YoAndChildHeavierThen4000g(mothers, newBorns);
        System.out.println("List with mothers above 25 years old and childs that are over 4000g weigth: " + motherabove + "\n");
    }


   public static List<Mother> parseMotherFileTxt() throws IOException {
        List<Mother> mothers = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\mamy.txt"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] s = line.split("\\s");
            mothers.add(new Mother(Integer.parseInt(s[0]), s[1], Integer.parseInt(s[2])));
        }
        bufferedReader.close();
        return mothers;
    }

    public static List<NewBorn> parseNewBornFileTxt() throws IOException {
        List<NewBorn> newBorn = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\noworodki.txt"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] s = line.split("\\s");
            newBorn.add(new NewBorn(Integer.parseInt(s[0]), s[1], s[2], Integer.parseInt(s[4]), Integer.parseInt(s[5]), Integer.parseInt(s[6])));
        }
        bufferedReader.close();
        return newBorn;
    }

    public static List<Mother> above25YoAndChildHeavierThen4000g(List<Mother> motherList, List<NewBorn> newBornList) {
        return motherList.stream()
                .filter(mother -> mother.getAge() > 25)//over 25yo
                .filter(mother -> mother .getMotherId() == newBorn.getId())//get mother that have same id as child so assuming that means that this is the mother of the child
                .filter(newBorn-> newBorn.getWeight() > 4000)//child over 4000g
                .collect(Collectors.toList());// I expect to collect all the filters and return the correct output : Example  Mother  is  : 112 Laura 38  
 and she have a child : 29 s Gabriel 1999-11-16 4100 54 112 = where  112 is the mother id that `I know is child of the mother` 

    }

I think something is wrong in the relation between the classes because I assume that the filter should work just fine if everything else is ok.

Normally should have mother has a list of children and a specific child has a field mother so with this I should be able to filter through.

I think you are looking for something like:

    public static List<Mother> above25YoAndChildHeavierThen4000g(List<Mother> motherList, List<NewBorn> newBornList) {
    return motherList.stream()
            .filter(mother -> mother.getAge() > 25)
            .filter(mother -> mother.getChildren().stream()
                    .anyMatch(child -> child.getWeight() > 4000))
            .collect(Collectors.toList());
    }

However the code overall definitely needs cleaning as advised in the comments

public List motherMoreThan() { List list = new ArrayList<>();

    for (Mother mother : mothers) {
        if (mother.getAge() > 25 && isChildOver4000(mother)) {
            list.add(mother);
        }
    }

    return list;
}

public boolean isChildOver4000(Mother mother) {
    for (Newborn newborn : mother.getList()) {
        if (newborn.getWeight() > 4000) {
            return true;
        }
    }

    return false;
}

you can call them like this:

System.out.println("\nMothers over 25 Years old with childer heavier than 4000g;");
        app.motherMoreThan()
                .forEach(System.out::println);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM