-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultMethodExample.java
More file actions
47 lines (38 loc) · 1.35 KB
/
Copy pathDefaultMethodExample.java
File metadata and controls
47 lines (38 loc) · 1.35 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
package defaultmethods;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import data.Student;
import data.StudentDB;
public class DefaultMethodExample {
public static Comparator<Student> sortByName=Comparator.comparing(Student::getName);
public static Comparator<Student> sortByGrade=Comparator.comparingDouble(Student::getGpa);
public static void main(String args[]) {
basic();
sortValues();
sortNullValue();
}
public static void sortNullValue() {
System.out.println(" **** Null Value *****");
Comparator<Student> allowNulls=Comparator.nullsFirst(sortByName);
List<Student> students=StudentDB.getAllStudents();
//students.add(null);
students.sort(allowNulls);
students.forEach(Student::printValue);
}
public static void sortValues() {
System.out.println("Sorting *****");
List<Student> students=StudentDB.getAllStudents();
students.sort(sortByName.thenComparing(sortByGrade));
students.forEach(Student::printValue);
}
public static void basic() {
List<String> listValue = Arrays.asList("c", "a", "b");
listValue.sort(Comparator.naturalOrder());
System.out.println(Arrays.toString(listValue.toArray()));
System.out.println("**Reverse Order**");
listValue = Arrays.asList("c", "b", "a");
listValue.sort(Comparator.reverseOrder());
System.out.println(Arrays.toString(listValue.toArray()));
}
}