-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem9.java
More file actions
67 lines (53 loc) · 1.52 KB
/
Problem9.java
File metadata and controls
67 lines (53 loc) · 1.52 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
66
67
package Problems;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/*
Sort employees by salary
*/
public class Problem9 {
public static void main(String[] args) {
ArrayList<Employee> list=new ArrayList<>();
list.add(new Employee("Alice",50000));
list.add(new Employee("bob",70000));
list.add(new Employee("rob",40000));
list.add(new Employee("john",10000));
List<Employee> sortedEmp = list.stream()
//sorted employee by name
.sorted((e1,e2)->e1.getName().compareTo(e2.getName()))
//sorted employees by salary
//.sorted((e1,e2)->Integer.compare(e1.getSalary(), e2.getSalary()))
//.sorted((e1, e2) -> e1.getSalary()- e2.getSalary())
.collect(Collectors.toList());
for(Employee e:sortedEmp){
System.out.println(e);
}
}
}
class Employee{
String name;
int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}