Iterating a list once instead of twice to get sum of each of two properties
I have JSON for students with marks. I need to get the total marks for each of maths and English. I could do it individually for each subject like below, but then the list is iterated twice. Is there a way to get both the marks by iterating the list once? Maths //for each standard , give the sum of Maths marks Map<Integer, Integer> mathsMarksPerStd = reportCard.getStudents().stream() .collect(Collectors.groupingBy(Student::getStandard, Collectors.reducing(0, st-> st.getMarks().getMaths(), Integer::sum))); English //for each standard , give the sum of Maths marks Map<Integer, Integer> engMarksP
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.