-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGetStudentGroup.java
More file actions
65 lines (50 loc) · 2.18 KB
/
GetStudentGroup.java
File metadata and controls
65 lines (50 loc) · 2.18 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package Online_Code_Samples.Week2;
import java.util.*;
public class GetStudentGroup {
public static Map<Integer, List<Student>> groupStudent(List<Student> students){
Map<Integer, List<Student>> groupedStudents = new HashMap<>();
List<Student> studentAbove70 = new ArrayList<>();
List<Student> cStudents = new ArrayList<>();
List<Student> backBenchers = new ArrayList<>();
for(Student st: students){
switch (st.getScore()) {
case 70 -> studentAbove70.add(st);
case 50 -> cStudents.add(st);
default -> backBenchers.add(st);
}
}
groupedStudents.put(70, studentAbove70);
groupedStudents.put(50, cStudents);
groupedStudents.put(40, backBenchers);
return groupedStudents;
}
public static void main(String[] args) {
Student first = new Student("John", 25, 70);
Student fifth = new Student("John", 25, 35);
Student second = new Student("Jane", 27, 70);
Student third = new Student("David", 35, 50);
Student fourth = new Student("Joy", 30, 45);
List<Student> student = new ArrayList<>();
student.add(first);
student.add(second);
student.add(third);
student.add(fourth);
student.add(fifth);
//Used to test for a condition: returns true or false;
// Predicate<Student> predicate = (stud) -> stud.getAge() < 30;
// student.stream().filter(predicate).forEach(System.out::println);
Comparator<Student> nameComparator = (s, s2) -> s.getName().compareTo(s2.getName());
Comparator<Student> scoreComparator = (s, s1 ) -> s.getScore() - s1.getScore();
Comparator<Student> ageComparator = (s, s1 ) -> s.getAge() - s1.getAge();
student.stream().sorted(ageComparator).forEach(System.out::println);
// Map<Integer, List<Student>> result = groupStudent(student);
//
// for( Integer key: result.keySet() ){
// System.out.println("Key is " + key);
// List<Student> list = result.get(key);
// for( Student st: list ){
// System.out.println(st);
// }
// }
}
}