Stream基础题目

1.题目要求

核⼼考点:类继承、Map 分组(部⻔→员⼯列表)、集合筛选、薪资统计题⽬要求:

  1. ⽗类 Person (姓名、年龄),⼦类 Employee 继承并新增:⼯号、部⻔、薪资;
  2. 将员⼯按部⻔分组存⼊ Map<String, List<Employee>> ;
  3. 统计每个部⻔的总薪资、平均薪资;
  4. 筛选出薪资>8000 的员⼯;
  5. 查询指定部⻔(技术部)的所有员⼯。

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);*/
相关推荐
程序员黑豆1 小时前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
GreenTea2 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
风流 少年2 小时前
Spring AI 2.0:Memory
java·后端·spring
码匠许师傅2 小时前
【C++ 面试真题】聊聊 C++ 的移动语义与右值引用
java·c++·面试
小席是个热心肠3 小时前
AI相关的自我学习
java·人工智能·学习
fqq33 小时前
力扣刷题前置Java语法
算法·leetcode·职场和发展
数智化管理手记5 小时前
海量数据如何沉淀有效数据资产?数据标准化治理方案如何搭建?
java·大数据·人工智能
2501_906565125 小时前
数学之美探究
算法
oier_Asad.Chen5 小时前
【洛谷题解/AcWing题解/OI学习笔记】洛谷P2868【USACO07DEC】Sightseeing Cows G(01分数规划求最大比率)
算法·图论·spfa·二分·负环
cfm_29145 小时前
SpringAI + Ollama 本地大模型
java·开发语言·人工智能·语言模型