-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamsComparatorExample.java
45 lines (36 loc) · 1.22 KB
/
StreamsComparatorExample.java
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
package com.learn.streams;
import com.learn.data.Student;
import com.learn.data.StudentDataBase;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class StreamsComparatorExample {
public static void main(String[] args) {
sortStudentsByName().forEach(System.out::println);
sortStudentsByGpa().forEach(System.out::println);
}
/**
* <p>
* Students need to be sorted by their name
* </p>
*/
public static List<Student> sortStudentsByName() {
System.out.println("Sorted by Name:");
return StudentDataBase.getAllStudents().stream()
.sorted(Comparator.comparing(Student::getName))
.collect(Collectors.toList());
}
/**
* <p>
* Sort the Students by their GPA in Decreasing Order.
* used reversed() to reverse the result and return the Collection.
* </p>
* @return
*/
public static List<Student> sortStudentsByGpa() {
System.out.println("Sorted By Gpa: ");
return StudentDataBase.getAllStudents().stream()
.sorted(Comparator.comparing(Student::getGpa).reversed())
.collect(Collectors.toList());
}
}