-
Notifications
You must be signed in to change notification settings - Fork 73
/
Problem4.java
80 lines (69 loc) · 2.5 KB
/
Problem4.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.github.hcsp.stream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Problem4 {
// 再用流的方法把之前的题目做一遍吧:
// 请编写一个方法,对传入的List<Employee>进行如下处理:
// 返回一个从部门名到这个部门的所有用户的映射。同一个部门的用户按照年龄进行从小到大排序。
// 例如,传入的employees是[{name=张三, department=技术部, age=40 }, {name=李四, department=技术部, age=30 },
// {name=王五, department=市场部, age=40 }]
// 返回如下映射:
// 技术部 -> [{name=李四, department=技术部, age=30 }, {name=张三, department=技术部, age=40 }]
// 市场部 -> [{name=王五, department=市场部, age=40 }]
public static Map<String, List<Employee>> collect(List<Employee> employees) {
return null;
}
public static void main(String[] args) {
System.out.println(
collect(
Arrays.asList(
new Employee(1, "张三", 40, "技术部"),
new Employee(2, "李四", 30, "技术部"),
new Employee(3, "王五", 40, "市场部"))));
}
static class Employee {
// 用户的id
private final Integer id;
// 用户的姓名
private final String name;
// 用户的年龄
private final int age;
// 用户的部门,例如"技术部"/"市场部"
private final String department;
Employee(Integer id, String name, int age, String department) {
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getDepartment() {
return department;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Employee person = (Employee) o;
return Objects.equals(id, person.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
}