Skip to content

Commit fb29eb2

Browse files
committed
groupingBy using Classifier, DownStream
1 parent 05df440 commit fb29eb2

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

Modern-Java-Examples/src/com/learn/streams_terminal/StreamsGroupingByExample.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public static void main(String[] args) {
2929

3030
System.out.println("Student grouped based on Gender : " + groupingByExample1());
3131
System.out.println("Student grouped based on PASS/FAIL : " + groupingByExample2());
32+
33+
System.out.println("Two Level Grouping : " + twoLevelGrouping1());
34+
System.out.println("Two Level Grouping " + twoLevelGrouping2());
3235
}
3336

3437
/**
@@ -54,4 +57,26 @@ public static Map<String, List<Student>> groupingByExample2() {
5457
.stream()
5558
.collect(Collectors.groupingBy(student -> student.getGpa() >= 3.8 ? "PASS" : "FAIL"));
5659
}
60+
61+
/**
62+
* <p>
63+
* Here, we are performing 2 Level grouping.
64+
* First Grouping is at the Grade Level.
65+
* Second Grouping is at a customized implementation (i.e. based on PASS ond FAIL ) on GPA.
66+
* </p>
67+
*/
68+
public static Map<Integer, Map<String, List<Student>>> twoLevelGrouping1() {
69+
return StudentDataBase.getAllStudents()
70+
.stream()
71+
.collect(Collectors.groupingBy(Student::getGradeLevel,
72+
Collectors.groupingBy(student -> student.getGpa() >= 3.8 ? "PASS" : "FAIL")));
73+
}
74+
75+
76+
public static Map<Integer, Integer> twoLevelGrouping2() {
77+
return StudentDataBase.getAllStudents()
78+
.stream()
79+
.collect(Collectors.groupingBy(Student::getGradeLevel,
80+
Collectors.summingInt(Student::getNoteBooks)));
81+
}
5782
}

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ This repository contains the basic &amp; advance level examples related to Java
4949
* [ minBy() and maxBy()](Modern-Java-Examples/src/com/learn/streams_terminal/StreamsMinByMaxByExample.java)
5050
* [ summingInt() and averagingInt()](Modern-Java-Examples/src/com/learn/streams_terminal/StreamsSumAvgExample.java)
5151
* [ groupingBy()](Modern-Java-Examples/src/com/learn/streams_terminal/StreamsGroupingByExample.java)
52-
5352

5453
<hr />
5554

0 commit comments

Comments
 (0)