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);*/
相关推荐
2501_9327502610 小时前
Java反射机制基础入门
java·开发语言
5008410 小时前
HCCL 集合通信编程:多卡协同的正确姿势
java·flutter·性能优化·electron·wpf
KaMeidebaby10 小时前
卡梅德生物技术快报|真核蛋白表达信号肽筛选实验全流程复盘
服务器·前端·数据库·人工智能·算法
霍霍的袁10 小时前
【C++初阶】函数重载详细讲解
开发语言·c++·算法
心中有国也有家10 小时前
CANN 算子开发完全指南——从 TBE DSL 到算子上线全流程
人工智能·经验分享·笔记·分布式·算法
阿文的代码库10 小时前
线段树入门:算法分析
数据结构·算法
asdfg125896310 小时前
Java中的Comparator 和JS中的回调函数好相似
java·开发语言
会编程的土豆10 小时前
消息队列(MQ)入门笔记
java·笔记·spring
水木流年追梦10 小时前
大模型入门-DPO 直接偏好优化
人工智能·学习·算法·机器学习·正则表达式