标签:out ffd 音乐 this 通过 有关 getname array 入参
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
// 老师 public class Teacher { private String id; private String name; private String subject; } // 学生 public class Student { private String id; private String name; private String techId; // 重写hashCode及equals方法(id及name相同就为同一个学生) } // 家长 public class Parents { private String id; private String name; private String chirldId; private String email; } // 课程 public enum Subject { private String value; private String desc; Subject(String value, String desc) { this.value = value; this.desc = desc; } Math("1", "数学"), Chinese("2", "汉语"), Music("3", "音乐"), English("4", "英语"); }
public class Test { // 模拟数据 public static Student s1 = new Student("1", "zhangsan", "001"); public static Student s2 = new Student("2", "lisi", "001"); public static Student s3 = new Student("3", "wangwu", "001"); public static Student s4 = new Student("4", "zhaoliu", "001"); public static Student s5 = new Student("6", "tianqi", "001"); public static Student s6 = new Student("6", "tianqi", "002"); public static Student s7 = new Student("6", "tianqi", "003"); public static Teacher t1 = new Teacher("001", "xiaoming", Subject.Math.getValue()); public static Teacher t2 = new Teacher("002", "lihua", Subject.Music.getValue()); public static Teacher t3 = new Teacher("003", "hanmeimei", Subject.Math.getValue()); public static Teacher t4 = new Teacher("004", "lihua", Subject.English.getValue()); public static List<Student> stus = new ArrayList<>(); public static List<Teacher> teacs = new ArrayList<>(); static { stus.add(s1); stus.add(s2); stus.add(s3); stus.add(s4); stus.add(s5); stus.add(s6); stus.add(s7); teacs.add(t1); teacs.add(t2); teacs.add(t3); teacs.add(t4); } public static void main(String[] args) { // 找到所有数学老师的学生的家长的电话,并找他们开家长会 List<Parents> collect = teacs.stream() // 过滤数学老师 .filter(t -> Subject.Math.getValue().equals(t.getSubject())) // 通过老师找学生 .flatMap(t -> stus.stream().filter(s -> s.getTechId().equals(t.getId()))) // 过滤重复的学生(使用student的equals和hashCode方法) .distinct() // 通过学生找家长(这里就简化为创建家长对象) .map(s -> { Parents p = new Parents(); p.setId(UUID.randomUUID().toString()); p.setChirldId(s.getId()); p.setName(s.getName().toUpperCase() + "‘s Parent"); p.setEmail((int) (Math.random() * 1000000) + "@qq.com"); return p; }) .collect(Collectors.toList()); // 打印到控制台看看 collect.stream() .forEach(System.out::println); } }
Parents{id=‘3d9312eb-0df5-4ec6-998f-94a32c2253b4‘, name=‘LISI‘s Parent‘, chirldId=‘2‘, telNo=‘844668@qq.com‘} Parents{id=‘7f0b92f5-872d-4671-982d-ef1b48840ce3‘, name=‘WANGWU‘s Parent‘, chirldId=‘3‘, telNo=‘563932@qq.com‘} Parents{id=‘c318bffd-8c6d-4849-8109-9c686c97fb77‘, name=‘ZHAOLIU‘s Parent‘, chirldId=‘4‘, telNo=‘108022@qq.com‘} Parents{id=‘a4ff1bbc-c9b6-4ad2-872c-f4df670c7bb6‘, name=‘TIANQI‘s Parent‘, chirldId=‘6‘, telNo=‘658956@qq.com‘}
public class Test { public static void main(String[] args) { List<Parents> pars = new ArrayList<>(); Set<Student> targetStudents = new HashSet<>(); for (Teacher t : teacs) { if (t.getSubject().equals(Subject.Math.getValue())) { for (Student s : stus) { if (s.getTechId().equals(t.getId())) { targetStudents.add(s); } } } } for (Student s : targetStudents) { Parents p = new Parents(); p.setId(UUID.randomUUID().toString()); p.setChirldId(s.getId()); p.setName(s.getName().toUpperCase() + "‘s Parent"); p.setEmail((int) (Math.random() * 1000000) + "@qq.com"); pars.add(p); } for (Parents p : pars) { System.out.println(p); } } }
标签:out ffd 音乐 this 通过 有关 getname array 入参
原文地址:https://www.cnblogs.com/xfyy-2020/p/13289066.html