neeraj bharti
neeraj bharti

Reputation: 361

How to return java bean object while doing multiple group by in stream

I have used multiple group by using java stream api but its result map function but unable to convert it on bean class. i am not familiar of writing collect complex objects using lambda.

Having issue with the function groupedStudentSubjects its not returning the List of Student objects with correct data i was not able to configure it. Need help on this.

.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());
    }
}`

Upvotes: 0

Views: 1266

Answers (1)

Hadi
Hadi

Reputation: 17289

IMO you can resolve your problem in simple way.just override equals() and hashCode() for student class all properties; and add some constructors and methods to class to simplify your stuffs.

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;
}

and

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

now you can use toMap collector with merge function.

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

and for create new Student object from previous result:

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

equals and 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());

and your solution is something like this:

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());

Upvotes: 3

Related Questions