使用 Streams 展平 Java 对象
Posted
技术标签:
【中文标题】使用 Streams 展平 Java 对象【英文标题】:Flatten Java Object using Streams 【发布时间】:2019-12-04 07:24:11 【问题描述】:我有这样的课:
class Student
String name;
String age;
List<Course> Courses;
class Course
String courseName;
String teacher;
我有:
List<Student> students
我想得到:
List<Object> objects
具有像这样的值:
Kevin 22 CS Rob
Kevin 22 MT Chris
Mary 21 AT Harry
Mary 21 AP Henry
Mary 21 PP Joe
Prak 25 null null
使用 Java 8 Streams
最简单的方法是什么?
可以flatMap
帮忙吗?
看看最后一个。即使Prak
没有参加任何课程(即List<Course>
为空),我也想获得记录,只有Student
以及其他课程
【问题讨论】:
【参考方案1】:请看下面的代码,我还在底部粘贴了输出。在这里,我们可以使用具有四个字符串实例变量的某个对象的 List 而不是使用 List。
List c1= Arrays.asList(new Course("CS", "Rob"), new Course("MT", "Chris"));
List<Course> c2= Arrays.asList(new Course("AT", "Harry"), new Course("AP","Henry"), new Course("PP", "Joe"));
List<Course> c3= Arrays.asList(new Course());
List<Student> s1= Arrays.asList(new Student("Kevin", "22", c1), new Student("Mary", "21", c2), new Student("Prak", "25", c3));
List<String> list= s1.stream().flatMap(s-> s.getCourses().stream().map(d1-> s.getName()+" "+s.getAge()+" "+""+d1.getCourseName()+" "+d1.getTeacher()) ).collect(Collectors.toList());
list.forEach( System.out::println);
输出:
凯文 22 CS Rob 凯文 22 MT 克里斯 玛丽 21 在哈利 玛丽 21 AP 亨利 玛丽 21 PP 乔 Prak 25 null null
【讨论】:
【参考方案2】:如果你想要一个新的对象扁平化,是的,flatMap 和 map 的组合可以工作,但是检查 List
是否为空:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test1
public static void main(String[] args)
Course course1 = new Course("course 1", "teacher 1");
Course course2 = new Course("course 2", "teacher 2");
Course course3 = new Course("course 3", "teacher 3");
Course course4 = new Course("course 4", "teacher 4");
Student student1 = new Student("name 1", "15", Arrays.asList(course1, course2, course3, course4));
Student student2 = new Student("name 2", "16", new ArrayList<>());
Student student3 = new Student("name 2", "16", Arrays.asList(course1, course3));
Student student4 = new Student("name 4", "17", new ArrayList<>());
List<Student> students = Arrays.asList(student1, student2, student3, student4);
List<Objects> objects = students.stream().flatMap(student ->
student.getCourses().isEmpty() ?
Stream.of(new Objects(student.getName(), student.getAge(), null, null)) :
student.getCourses().stream().map(course -> new Objects(student.getName(), student.getAge(), course.getCourseName(), course.getTeacher()))
).collect(Collectors.toList());
objects.forEach(System.out::println);
class Course
String courseName;
String teacher;
public Course(String courseName, String teacher)
this.courseName = courseName;
this.teacher = teacher;
public String getCourseName()
return courseName;
public void setCourseName(String courseName)
this.courseName = courseName;
public String getTeacher()
return teacher;
public void setTeacher(String teacher)
this.teacher = teacher;
class Student
String name;
String age;
List<Course> courses;
public Student(String name, String age, List<Course> courses)
this.name = name;
this.age = age;
this.courses = courses;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getAge()
return age;
public void setAge(String age)
this.age = age;
public List<Course> getCourses()
return courses;
public void setCourses(List<Course> courses)
this.courses = courses;
class Objects
String name;
String age;
String courseName;
String teacher;
public Objects(String name, String age, String courseName, String teacher)
this.name = name;
this.age = age;
this.courseName = courseName;
this.teacher = teacher;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getAge()
return age;
public void setAge(String age)
this.age = age;
public String getCourseName()
return courseName;
public void setCourseName(String courseName)
this.courseName = courseName;
public String getTeacher()
return teacher;
public void setTeacher(String teacher)
this.teacher = teacher;
@Override
public String toString()
return "name: " + this.name + " age: " + this.age + " course name: " + this.getCourseName() + " teacher: " + this.getTeacher();
输出
name: name 1 age: 15 course name: course 1 teacher: teacher 1
name: name 1 age: 15 course name: course 2 teacher: teacher 2
name: name 1 age: 15 course name: course 3 teacher: teacher 3
name: name 1 age: 15 course name: course 4 teacher: teacher 4
name: name 2 age: 16 course name: null teacher: null
name: name 2 age: 16 course name: course 1 teacher: teacher 1
name: name 2 age: 16 course name: course 3 teacher: teacher 3
name: name 4 age: 17 course name: null teacher: null
【讨论】:
以上是关于使用 Streams 展平 Java 对象的主要内容,如果未能解决你的问题,请参考以下文章
Kafka Streams应用程序在kafka服务器上打开了太多文件