-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathInterfaces.java
40 lines (31 loc) · 1.14 KB
/
Interfaces.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
package streams;
import java.util.Comparator;
public class Interfaces {
public static void main(String[] args) {
Employee mike = new Employee("Mike", 2000),
louise = new Employee("Louise", 2500);
Comparator<Employee> byName = new Comparator<Employee>() {
public int compare(Employee a, Employee b) {
return a.getName().compareTo(b.getName());
}
};
System.out.println("By name:");
System.out.println(byName.compare(mike, louise));
try {
// throws NPE
System.out.println(byName.compare(mike, null));
} catch (NullPointerException e) {
System.out.println(e);
}
// a static method in Comparator
Comparator<Employee> byNameThenNull = Comparator.nullsLast(byName);
System.out.println("Then null:");
System.out.println(byNameThenNull.compare(mike, louise));
System.out.println(byNameThenNull.compare(mike, null));
// a default method in Comparator
Comparator<Employee> nullThenByDecreasingName = byNameThenNull.reversed();
System.out.println("Reversed:");
System.out.println(nullThenByDecreasingName.compare(mike, louise));
System.out.println(nullThenByDecreasingName.compare(mike, null));
}
}