1.题目要求
核⼼考点:类继承、Map 分组(部⻔→员⼯列表)、集合筛选、薪资统计题⽬要求:
- ⽗类 Person (姓名、年龄),⼦类 Employee 继承并新增:⼯号、部⻔、薪资;
- 将员⼯按部⻔分组存⼊ Map<String, List<Employee>> ;
- 统计每个部⻔的总薪资、平均薪资;
- 筛选出薪资>8000 的员⼯;
- 查询指定部⻔(技术部)的所有员⼯。
2.题目解析
package Day521;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class Person {
private String name;
private int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
}
class Employee extends Person{
private String id;
private String Sname;
private double salary;
@Override
public String toString() {
return "员工{" +
"姓名='" + getName() + '\'' + // 父类的name,用getName()获取
",部门='" + Sname + '\'' + // Sname实际是部门名,改成“部门”
",薪资=" + salary +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Employee employee = (Employee) o;
return Double.compare(salary, employee.salary) == 0 && Objects.equals(id, employee.id) && Objects.equals(Sname, employee.Sname);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), id, Sname, salary);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee(String name, int age, String id, String sname, double salary) {
super(name, age);
this.id = id;
Sname = sname;
this.salary = salary;
}
}
class Test{
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("张三", 25, "E001", "技术部", 9000.0));
employeeList.add(new Employee("李四", 28, "E002", "技术部", 12000.0));
employeeList.add(new Employee("王五", 30, "E003", "行政部", 6000.0));
employeeList.add(new Employee("赵六", 26, "E004", "市场部", 10000.0));
employeeList.add(new Employee("孙七", 29, "E005", "市场部", 7500.0));
//总薪资
Map<String, List<Employee>> deptMap = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getSname));
System.out.println("===== 1. 按部门分组(Stream) =====");
for (Map.Entry<String,List<Employee>> entry : deptMap.entrySet()){
String dept = entry.getKey();
List<Employee> employees = entry.getValue();
//总薪资
double total = employees.stream()
.mapToDouble(Employee::getSalary)
.sum();
//平均数
double avg = employees.stream()
.mapToDouble(Employee::getSalary)
.average()
.orElse(0);
System.out.printf("部门:%s | 总薪资:%.2f | 平均薪资:%.2f%n",dept,total,avg);
}
//薪资>8000
System.out.println("========薪资>80000========");
List<Employee> dy = employeeList.stream()
.filter(employee -> employee.getSalary()>8000)
.collect(Collectors.toList());
dy.forEach(System.out::println);
//查询技术部的所有员工
System.out.println("======技术部员工====");
List<Employee> cx = employeeList.stream()
.filter(d -> d.getSname().equals("技术部"))
.collect(Collectors.toList());
cx.forEach(System.out::println);
}
}
3.难点解析
/*
* // 1. 遍历 Map 里的每一组(部门 = key,员工列表 = value)
for (Map.Entry<String, List<Employee>> entry : groupByDept.entrySet()) {
// 2. 拿到当前部门名称(key)
String dept = entry.getKey();
// 3. 拿到这个部门的所有员工(value 是 List<Employee>)
List<Employee> empList = entry.getValue();
// 4. 计算这个部门【总薪资】
double total = empList.stream()
.mapToDouble(Employee::getSalary) // 把每个员工 → 转成薪资数字
.sum(); // 数字求和
// 5. 计算这个部门【平均薪资】
double avg = empList.stream()
.mapToDouble(Employee::getSalary) // again 转成薪资数字
.average() // 求平均值
.orElse(0); // 没人就返回0,防止报错
// 6. 格式化输出:部门名、总薪资(保留2位小数)、平均薪资
System.out.printf("部门:%s | 总薪资:%.2f | 平均薪资:%.2f%n",
dept, total, avg);*/