Karthik
Karthik

Reputation: 27

Group a list of objects by Unique key as a object using Java API

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;

}

Upvotes: 0

Views: 88

Answers (1)

Sourav Jha
Sourav Jha

Reputation: 411

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

Upvotes: 2

Related Questions