如何在流中进行多个分组时返回Java bean对象

时间:2019-05-11 11:24:54

标签: java-8 java-stream grouping

我通过使用java流api使用了多个组,但是它的结果映射功能却无法在bean类上进行转换。我不熟悉使用lambda编写收集复杂对象的方法。

功能groupedStudentSubjects出现问题,未返回带有正确数据的Student对象列表,我无法对其进行配置。 在这方面需要帮助。

.flatMap(e -> e.getValue().entrySet().stream()
                    .map(a -> new Student(e.getKey(), a.getKey(), 
                          a.getKey(),a.getKey(),a.getKey(),a.getKey())))
public class Student {
       String firstName;
       String Lastname;
       String id;
       String class_name;
       String section;
       String subject;
...
}

List<Student> students = new ArrayList<>();    
       students.add(new Student("Ram", "Kumar", "123", "12", "A", "MATH"));
       students.add(new Student("Ram", "Kumar", "123", "12", "A", "SCIENCE"));
       students.add(new Student("Ram", "Kumar", "123", "12", "A", "ENGLISH"));
       students.add(new Student("Shyam", "Kumar", "124", "12", "A", "MATH"));
       students.add(new Student("Shyam", "Kumar", "124", "12", "A", "SCIENCE"));
       students.add(new Student("Shyam", "Kumar", "124", "12", "A", "ENGLISH"));

public static List<Student> groupedStudentSubjects(List<Student> students) {

    return  students.stream()
                .collect(Collectors.groupingBy(Student::getFirstName,
                                Collectors.groupingBy(Student::getLastname,
                                        Collectors.groupingBy(Student::getId,
                                        Collectors.groupingBy(Student::getClass_name,
                                        Collectors.groupingBy(Student::getSection,
                                        Collectors.mapping(Student::getSubject, Collectors.joining(","))))))))

                .entrySet()
                .stream()
                .flatMap(e -> e.getValue().entrySet().stream()
                        .map(a -> new Student(e.getKey(), a.getKey(), a.getKey(),a.getKey(),a.getKey(),a.getKey())))
                .collect(Collectors.toList());
    }
}`

1 个答案:

答案 0 :(得分:3)

IMO,您可以通过简单的方法解决问题。只需覆盖equals()hashCode()到学生班的所有属性;并在类中添加一些构造函数和方法以简化内容。

public Student(Student student) {
    this.firstName = student.firstName;
    this.lastname = student.lastname;
    this.id = student.id;
    this.class_name = student.class_name;
    this.section = student.section;
}

public Student setSubject(String subject) {
    this.subject = subject;
    return this;
}

现在您可以将toMap收集器与合并功能一起使用。

 Map<Student,String> map =
         students.stream()
            .collect(Collectors
                .toMap(Student::new, Student::getSubject, (v1, v2) -> v1.concat(",").concat(v2)));

并根据先前结果创建新的Student对象:

List<Student> result = map.entrySet().stream()
            .map(entry -> new Student(entry.getKey()).setSubject(entry.getValue()))
            .collect(Collectors.toList()); 

等于和hashCode:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Student student = (Student) o;
    return Objects.equals(firstName, student.firstName) &&
            Objects.equals(lastname, student.lastname) &&
            Objects.equals(id, student.id) &&
            Objects.equals(class_name, student.class_name) &&
            Objects.equals(section, student.section);
}

@Override
public int hashCode() {
    return Objects.hash(firstName, lastname, id, class_name, section);
}

students.stream()
            .collect(Collectors.groupingBy(Student::new, Collectors.mapping(Student::getSubject, Collectors.joining(","))))
            .entrySet()
            .stream()
            .map(entry -> new Student(entry.getKey()).setSubject(entry.getValue()))
            .collect(Collectors.toList());

您的解决方案是这样的:

students.stream()
            .collect(Collectors.groupingBy(Student::getFirstName,
                    Collectors.groupingBy(Student::getLastname,
                            Collectors.groupingBy(Student::getId,
                                    Collectors.groupingBy(Student::getClass_name,
                                            Collectors.groupingBy(Student::getSection,
                                                    Collectors.mapping(Student::getSubject, Collectors.joining(","))))))))
            .entrySet()
            .stream()
            .flatMap(entry->entry.getValue().entrySet().stream()
            .flatMap(entry2->entry2.getValue().entrySet().stream()
            .flatMap(entry3->entry3.getValue().entrySet().stream()
            .flatMap(entry4->entry4.getValue().entrySet().stream()
            .map(entry5->new Student(entry.getKey(),entry2.getKey(),entry3.getKey(),entry4.getKey(),entry5.getKey(),entry5.getValue()))))))
            .collect(Collectors.toList());