标签:salary,Java,name,Stream,getSalary,案例,getBonus,public,e2 From: https://www.cnblogs.com/liandaozhanshi/p/17591360.htmlJava之Stream流综合案例
需求:
某个公司的开发部门,分为开发一部和二部,现在需要进行年中数据结算。
分析:
员工信息至少包含了(名称、性别、工资、奖金、处罚记录)
开发一部有4个员工,开发二部有5个员工。
分别筛选出2个部门的最高工资的员工信息,封装成优秀员工对象。
分别统计出2个部门的平均月收入,要求去掉最高和最低工资
统计2个开发部门整体的平均工资去掉最低和最高工资的平均值
代码实现
StreamDemo04类
public class StreamDemo04 { public static double allMoney; public static double allMoney2; public static double allMoney3; public static void main(String[] args) { List<Employee> one = new ArrayList<>(); one.add(new Employee("猪八戒",'男',30000,25000,null)); one.add(new Employee("孙悟空",'男',25000,1000,"顶撞上司")); one.add(new Employee("沙僧",'男',20000,20000,null)); one.add(new Employee("小白龙",'男',20000,25000,null)); List<Employee> two = new ArrayList<>(); two.add(new Employee("武松",'男',15000,9000,null)); two.add(new Employee("李逵",'男',20000,10000,null)); two.add(new Employee("西门庆",'男',50000,100000,"被打")); two.add(new Employee("潘金莲",'女',2500,1000,"被打")); two.add(new Employee("武大郎",'女',20000,0,"下毒")); //1、开发一部的最高工资的员工。 // 指定大小规则了 // Employee e = one.stream().max((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus())).get(); // System.out.println(e); Topperformer t = one.stream().max((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus())) .map(e->new Topperformer(e.getName(),e.getSalary()+e.getBonus())).get(); System.out.println("开发一部优秀员工是:"+t); Topperformer t1 = one.stream().max((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus())) .map(e->new Topperformer(e.getName(),e.getSalary()+e.getBonus())).get(); System.out.println("开发二部优秀员工是:"+t1); //2、统计平均工资,去掉最高工资和最低工资 one.stream().sorted((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus() , e2.getSalary()+e2.getBonus())) .skip(1).limit(one.size() - 2).forEach(e ->{ //求出总和:剩余员工的工资总和 allMoney += (e.getSalary() + e.getBonus()); }); System.out.println("开发一部的平均工资是:"+allMoney / (one.size() - 2)); //3、开发二部的平均工资 two.stream().sorted((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus() , e2.getSalary()+e2.getBonus())) .skip(1).limit(two.size() - 2).forEach(e->{ //求出总和:剩余员工工资总和 allMoney2 += e.getSalary()+e.getBonus(); }); BigDecimal x = BigDecimal.valueOf(allMoney2); BigDecimal y = BigDecimal.valueOf(two.size() -2); System.out.println("开发二部的平均工资是:"+x.divide(y,2,RoundingMode.HALF_UP)); //开发部的平均工资 //合并stream流 Stream<Employee> s = one.stream(); Stream<Employee> s1 = two.stream(); Stream<Employee> s2 = Stream.concat(s,s1); s2.sorted((e1,e2)->Double.compare(e1.getSalary()+ e1.getBonus() , e2.getSalary()+ e2.getBonus())) .skip(1).limit(one.size() + two.size() -2).forEach(e->{ //求出总和:剩余员工工资总和 allMoney3 += e.getSalary()+e.getBonus(); }); //BigDecimal BigDecimal a = BigDecimal.valueOf(allMoney3); BigDecimal b = BigDecimal.valueOf(one.size() +two.size() -2); System.out.println("开发部的平均工资是:"+a.divide(b,2, RoundingMode.HALF_UP)); //意思是 a÷b 保留两位小数 四舍五入 } }
优秀员工类(Topperformer)
public class Topperformer { String name; double salary; public Topperformer(String name, double salary) { this.name = name; this.salary = salary; } public Topperformer() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "name='" + name + '\'' + ", salary=" + salary ; } }
Employee类(员工类)
package com.itheng.d2_stream; public class Employee { private String name; private char sex; private double salary; private double bonus; private String punish; //处罚信息 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 Employee() { } 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 "姓名='" + name + '\'' + ", 性别=" + sex + ", 薪水=" + salary + ", 津贴=" + bonus + ", 处罚='" + punish + '\''; } }