Repository navigation
Expand file tree
/
Copy pathEmployee.java
More file actions
58 lines (46 loc) · 1.61 KB
/
Copy pathEmployee.java
File metadata and controls
58 lines (46 loc) · 1.61 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
package corejava.streams.sorting;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Employee implements Comparable<Employee> {
public static final Comparator<Employee> BY_NAME_USERNAME_AGE =
Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getUsername)
.thenComparingInt(Employee::getAge);
private final String firstName;
private final String username;
private final int age;
public Employee(String firstName, String username, int age) {
this.firstName = firstName;
this.username = username;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public String getUsername() {
return username;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Employee other) {
return BY_NAME_USERNAME_AGE.compare(this, other);
}
@Override
public String toString() {
return String.format("%-8s %-10s %3d", firstName, username, age);
}
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>(List.of(
new Employee("Ravi", "ravi.s", 41),
new Employee("Anita", "anita.p", 29),
new Employee("Ravi", "ravi.k", 30),
new Employee("Ravi", "ravi.k", 24),
new Employee("Bhavesh", "bhavesh.j", 52),
new Employee("Anita", "anita.p", 22)));
employees.sort(BY_NAME_USERNAME_AGE);
employees.forEach(System.out::println);
}
}