stream().类 之间的过滤器
stream().filter between Classes
我有 2 个 类
母亲和新生儿
Class妈妈:
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 新生儿:
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 +
'}';
}
}
我必须让妈妈比 25 岁大 child 超过 4000 克体重
我做了以下事情
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`
}
我认为 类 之间的关系有问题,因为我认为如果其他一切正常,过滤器应该可以正常工作。
通常应该有 mother 有一个 children 的列表,一个特定的 child 有一个 field mother 所以我应该可以过滤。
我想你正在寻找类似的东西:
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());
}
但是,按照评论中的建议,整体代码肯定需要清理
public 列出 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;
}
你可以这样称呼他们:
System.out.println("\nMothers over 25 Years old with childer heavier than 4000g;");
app.motherMoreThan()
.forEach(System.out::println);
我有 2 个 类 母亲和新生儿 Class妈妈:
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 新生儿:
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 +
'}';
}
}
我必须让妈妈比 25 岁大 child 超过 4000 克体重
我做了以下事情
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`
}
我认为 类 之间的关系有问题,因为我认为如果其他一切正常,过滤器应该可以正常工作。
通常应该有 mother 有一个 children 的列表,一个特定的 child 有一个 field mother 所以我应该可以过滤。
我想你正在寻找类似的东西:
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());
}
但是,按照评论中的建议,整体代码肯定需要清理
public 列出 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;
}
你可以这样称呼他们:
System.out.println("\nMothers over 25 Years old with childer heavier than 4000g;");
app.motherMoreThan()
.forEach(System.out::println);