Java basics - a comprehensive case of Stream flow

case:

The development department of a certain company is divided into the first development department and the second development department, and now it needs to conduct mid-year data settlement.

analyze:

  1. Employee information includes at least (name, gender, salary, bonus, punishment records).
  2. The development department has one employee, and the development department has five employees.
  3. Screen out the employee information with the highest salary in the two departments respectively, and package it into an excellent employee object Topperformer.
  4. The average monthly income of the two departments is calculated separately, and the highest and minimum wages are required to be removed.
  5. Calculate the average salary of the two development departments as a whole, and remove the average of the minimum and maximum salaries.

 Employee information class:

public class Employee {
    private String name;//名称
    private char sex;//性别
    private double salary;//工资
    private double bonus;//奖金
    private String punish;//处罚记录

    public Employee() {
    }

    public Employee(String name, char sex, double salary, double bonus, String punish) {
        this.name = name;
        this.sex = sex;
        this.salary = salary;
        this.bonus = bonus;
        this.punish = punish;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    public String getPunish() {
        return punish;
    }

    public void setPunish(String punish) {
        this.punish = punish;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", salary=" + salary +
                ", bonus=" + bonus +
                ", punish='" + punish + '\'' +
                '}';
    }
}

Encapsulated into an excellent employee object Topperformer:

public class Topperformer {
    private String name;
    private double money;//月薪

    public Topperformer() {
    }

    public Topperformer(String name, double money) {
        this.name = name;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Topperformer{" +
                "name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

Case implementation: 

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
//import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
 * Stream的综合案例:某公司的开发部门,分为开发一部和开发二部,现在需要进行年中数据结算。
 * 1.员工信息至少包含了(名称,性别,工资,奖金,处罚记录)。
 * 2.开发一部个员工,开发二部有5名员工。
 * 3.分别筛选出2个部门的最高工资的员工信息,封装成优秀员工对象Topperformer。
 * 4.分别统计出2个部门的平均月收入,要求去掉最高和最低工资。
 * 5.统计2个开发部门整体的平均工资,去掉最低和最高工资的平均值。
 */
public class StreamDemo {
    //4.创建共享变量,用于统计出2个部门的平均月收入中的求和
    public static double allMoney;
    public static double allMoney2;//两个部门综合

    public static void main(String[] args) {
        //2.定义两个部门
        //2.1开发一部
        List<Employee> one = new ArrayList<>();
        one.add(new Employee("小小1",'男',30000,25000,null));
        one.add(new Employee("小小2",'男',25000,1000,"顶撞上司"));
        one.add(new Employee("小小3",'男',20000,20000,null));
        one.add(new Employee("小小4",'男',20000,25000,null));
        //2.2开发二部
        List<Employee> two = new ArrayList<>();
        two.add(new Employee("大大1",'男',15000,9000,null));
        two.add(new Employee("大大2",'男',25000,10000,null));
        two.add(new Employee("大大3",'男',50000,100000,"顶撞上司"));
        two.add(new Employee("大大4",'女',3500,1000,"顶撞上司"));
        two.add(new Employee("大大4",'女',20000,0,"顶撞上司"));

        //3.筛选最高工资员工 ==》制定大小规则
//        System.out.println(one.stream().max(new Comparator<Employee>() {
//            @Override
//            public int compare(Employee e1, Employee e2) {
//                return Double.compare(e1.getSalary() + e1.getBonus(), e2.getSalary() + e2.getBonus());
//            }
//        }).get());
        //Employee{name='小小1', sex=男, salary=30000.0, bonus=25000.0, punish='null'}

        //简化:
        //        System.out.println(one.stream().max(( e1,  e2) -> Double.compare(e1.getSalary() + e1.getBonus(), e2.getSalary() + e2.getBonus())).get());
        Employee e = one.stream().max((e1,e2)->Double.compare(e1.getSalary()+ e1.getBonus(), e2.getSalary()+ e2.getBonus())).get();
        System.out.println(e);//Employee{name='小小1', sex=男, salary=30000.0, bonus=25000.0, punish='null'}

        Employee ee = two.stream().max((e1,e2)->Double.compare(e1.getSalary()+ e1.getBonus(), e2.getSalary()+ e2.getBonus())).get();
        System.out.println(ee);//Employee{name='大大3', sex=男, salary=50000.0, bonus=100000.0, punish='顶撞上司'}

        //封装成优秀员工对象Topperformer==>使用map加工
        Topperformer t = one.stream().max((e1,e2)->Double.compare(e1.getSalary()+ e1.getBonus(), e2.getSalary()+ e2.getBonus()))
                .map(e1 -> new Topperformer(e1.getName(),e1.getSalary()+e1.getBonus())).get();
        System.out.println(t);//Topperformer{name='小小1', money=55000.0}

        Topperformer tt = two.stream().max((e1,e2)->Double.compare(e1.getSalary()+ e1.getBonus(), e2.getSalary()+ e2.getBonus()))
                .map(e1 -> new Topperformer(e1.getName(),e1.getSalary()+e1.getBonus())).get();
        System.out.println(tt);//Topperformer{name='大大3', money=150000.0}

        //4.分别统计出2个部门的平均月收入,要求去掉最高和最低工资。
        one.stream().sorted((e1,e2) -> Double.compare(e1.getSalary()+ e1.getBonus(), e2.getSalary()+ e2.getBonus()))
                .skip(1).limit(one.size() - 2).forEach(e1 ->{
                    //求出总和,剩余工资的总和
                    allMoney += (e1.getSalary()+e1.getBonus());
                });
        System.out.println("开发一部的平均工资是:" + allMoney/(one.size() - 2));

        two.stream().sorted((e1,e2) -> Double.compare(e1.getSalary()+ e1.getBonus(), e2.getSalary()+ e2.getBonus()))
                .skip(1).limit(two.size() - 2).forEach(e1 ->{
                    //求出总和,剩余工资的总和
                    allMoney += (e1.getSalary()+e1.getBonus());
                });
        //System.out.println("开发二部的平均工资是:" + allMoney/(two.size() - 2));
        //开发二部的平均工资是:54666.666666666664 ==》精度失真 ==》解决办法如下
        BigDecimal a = BigDecimal.valueOf(allMoney);
        BigDecimal b = BigDecimal.valueOf(two.size() - 2);
        System.out.println("开发二部的平均工资是:" + a.divide(b,2, RoundingMode.HALF_UP));//结果四舍五入保留两位小数
        //开发二部的平均工资是:54666.67


        //5.统计2个开发部门整体的平均工资,去掉最低和最高工资的平均值。
        //5.1合并两个集合流,再统计
        Stream<Employee> s1 = one.stream();
        Stream<Employee> s2 = two.stream();
        Stream<Employee> s3 = Stream.concat(s1,s2);
        s3.sorted((e1,e2) -> Double.compare(e1.getSalary()+ e1.getBonus(), e2.getSalary()+ e2.getBonus()))
                .skip(1).limit((one.size()+two.size()) - 2).forEach(e1 ->{
                    //求出总和,剩余工资的总和
                    allMoney2 += (e1.getSalary()+e1.getBonus());
                });
        System.out.println("2个开发部门的平均工资是:" + allMoney2/((one.size()+two.size()) - 2));

    }
}

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/130073403
Recommended