forked from greasymolue/SF-Int-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingCollectors.java
More file actions
48 lines (42 loc) · 1.6 KB
/
UsingCollectors.java
File metadata and controls
48 lines (42 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package streams;
import students.Student;
import java.sql.SQLOutput;
import java.util.List;
import java.util.stream.Collectors;
public class UsingCollectors {
public static String letterGrade(Student s) {
double gpa = s.getGpa();
if (gpa > 3.75) return "A";
if (gpa > 3.35) return "B";
if (gpa > 2) return "C";
return "D";
}
public static void main(String[] args) {
List<Student> roster = List.of(
Student.of("Fred", 3.4, "Math", "Physics"),
Student.of("Jim", 2.4, "Journalism"),
Student.of("Jimmy", 2.5, "Journalism"),
Student.of("James", 2.3, "Journalism"),
Student.of("Jimbo", 3.5, "Journalism"),
Student.of("Jimmina", 3.8, "Journalism"),
Student.of("Sheila", 3.9, "Math", "Physics", "Quantum Mechanics", "Astrophysics")
);
var map = roster.stream()
.collect(Collectors.groupingBy(s -> letterGrade(s)));
map.entrySet().stream()
.map(e -> "Students with grade " + e.getKey() + " are " + e.getValue())
.forEach(s -> System.out.println(s));
roster.stream()
.collect(Collectors.groupingBy(s -> letterGrade(s), Collectors.counting()))
.entrySet().stream()
.map(e -> e.getValue() + " students scored grade " + e.getKey())
.forEach(s -> System.out.println(s));
roster.stream()
.collect(Collectors.groupingBy(s -> letterGrade(s),
Collectors.mapping(s -> s.getName(),
Collectors.joining(", "))))
.entrySet().stream()
.map(e -> e.getValue() + " scored grade " + e.getKey())
.forEach(s -> System.out.println(s));
}
}