Better way of handling groupingBy and mapping in Java

29 views Asked by At

I have this json for student marks and I want to get count of students from each standard who has programming in hobby...I have written this code which is not working..it is showing all the students from a standard. Requesting help. Thanks in advance...

json

{
  "students": [
    {
      "fullName": "Student 1",
      "standard": 10,
      "rollNumber": "1R",
      "house": "Red",
      "marks": {
        "maths": 90,
        "english": 90,
        "science": 80,
        "history": 90
      },
      "totalMarks": 350,
      "sports": [
        {
          "sportName": "karate",
          "status": "purple-belt"
        },
        {
          "sportName": "swimming",
          "status": "swimmer"
        },
        {
          "sportName": "karate",
          "status": "green-belt"
        }
      ],
      "hobbies": [
        "programming",
        "reading"
      ]
    },
    {
      "fullName": "Student 2",
      "standard": 10,
      "rollNumber": "2R",
      "house": "Blue",
      "marks": {
        "maths": 90,
        "english": 90,
        "science": 90,
        "history": 90
      },
      "totalMarks": 360,
      "sports": [
        {
          "sportName": "cricket",
          "status": "all-rounder"
        }
      ],
      "hobbies": [
        "gaming"
      ]
    }
}

**

Code

 Map<Integer, Long> map10 = reportCard.getStudents().stream().collect(Collectors.groupingBy(Student::getStandard, Collectors.mapping(student -> student.getHobbies().stream().filter(hobby -> hobby.equalsIgnoreCase("programming")),Collectors.counting())));
        System.out.println("map10");
        map10.forEach((key, value) -> System.out.println(key + "-" + value));
0

There are 0 answers