0

I am trying to create a key and value relationship and return in a json format between list of data returned by Database. I'm able to achieve the relationship but not able to get the desired Json Format as a output

StudentProfResponse = profRepository.getstudentProfessorHirarchy();

studentprofResponse.stream().collect(Collectors.groupingBy(StudentProfResponse::getProfessorName));

Db Results

ProfessorName ProfessorId StudentId StudentName

Vinay P123 S567 Karthik
Vinay P123 S568 Jeevan
Mayank P657 S569 Meena
Vasu P723 S570 Vijay

Json Format Im getting with the above code

[
{
    "vinay": [
        {
            "studentId": "S567",
            "StudentName": "Karthik",
            "professorName": "Vinay",
            "professorId": "P123"
        },
        {
            "studentId": "S568",
            "StudentName": "Jeevan",
            "professorName": "Vinay",
            "professorId": "P123"
        }
    ]
},
{
    "Mayank": [
        {
            "studentId": "S569",
            "StudentName": "Meena",
            "professorName": "Mayank",
            "professorId": "P657"
        }
    ]
},
{
    "vasu": [
        {
            "studentId": "S570",
            "StudentName": "Vijay",
            "professorName": "Vasu",
            "professorId": "P723"
        }
    ]
}

]

Json format im Expecting

[
 {
    "professorName": "Vinay",
    "professorId" : "P123",
    "studentDetails": [
        {
            "studentId": "S567",
            "StudentName": "Karthik"
        },
        {
            "studentId": "S568",
            "StudentName": "Jeevan"
        }
    ]
},
{
    "professorName": "Mayank",
    "professorId" : "P657",
    "studentDetails": [
        {
            "studentId": "S569",
            "StudentName": "Meena"
        }
    ]
},
{
    "professorName": "Vasu",
    "professorId" : "P723",
    "studentDetails": [
        {
            "studentId": "S570",
            "StudentName": "Vijay"
        }
    ]
 }
]

Is there a way, I can achieve above Json Format through Java 8 stream API by creating a bean class like and send a List of studprofrelationship details

public class StudentProfessorRelationship {

private Integer professorNo;

private String professorName;

private List<StudentDetails> studentDetails;

}
1
  • Could you provider code for the class StudentProfResponse ? Why you absolutely Need streams ? You can do that with old vanilla Java Commented Feb 16, 2020 at 12:04

1 Answer 1

2

We can use Collectors.toMap with mergeFunction parameter of Collectors.toMap(keyMapper, valueMapper, mergeFunction):

  studentprofResponse.stream()
    .map(resp-> new StudentProfessorRelationship(/*add arguments*/) )
    .collect(
             Collectors.toMap( 
                              StudentProfessorRelationship::getProfessorName, 
                              Function.identity(),
                              (relationship1, relationship2) -> {  
                                      relationship1.getstudentDetails().addAll(
                                           relationship2.getstudentDetails()); 
                                       return realtionship1;} ))  
      .values();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Sourav, It's Working. U should keep relationship1.getstudentDetails() instead of relationship
is there anyway if i want to accommodate list of courses for each student in studentdetails class in the same solution u have provided ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.