Stream流综合案例

 

 

public class Anli {
    public static double SumSalary = 0;
    public static double SumSalary2 = 0;
    public static void main(String[] args) {
        int i = 0;
        List<Employee> xiyou = new ArrayList<>();
        Collections.addAll(xiyou,
                new Employee("猴子", '1', 10000.0, 500.0, "kill"),
                new Employee("pig", '1', 3000.0, 100.0, null),
                new Employee("Sha", '1', 2000.0, 100.0, null),
                new Employee("JinChan", '0', 100000.0, 1000.0, "kindness"));
        List<Employee> wangzhe = new ArrayList<>();
        Collections.addAll(wangzhe,
                new Employee("元芳", '1', 30000.0, 500.0, "kill"),
                new Employee("鲁班", '1', 1000.0, 100.0, null),
                new Employee("貂蝉", '0', 20000.0, 1000.0, null),
                new Employee("米莱迪", '0', 8000.0, 2000.0, null));
        //1.工资最高
        Employee e = xiyou.stream().max(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return Double.compare(o1.getSalary()+o1.getBonus(), o2.getSalary()+o2.getBonus());
            }
        }).get();
        System.out.println(e);
        //2.封装成新对象Niubier
        Niubier nb = wangzhe.stream().max((o1, o2) -> Double.compare(o1.getSalary()+o1.getBonus(), o2.getSalary()+o2.getBonus()))
                .map(employee -> new Niubier(employee.getName(), employee.getSalary()+employee.getBonus()))
                .get();
        System.out.println(nb);
        //3.平均工资,去掉高低
        xiyou.stream()
                .sorted(((o1, o2) -> Double.compare(o1.getSalary()+o1.getBonus(), o2.getSalary()+o2.getBonus())))
                .skip(1).limit(xiyou.size()-2)
                .forEach(e1 -> {SumSalary+=e1.getSalary()+e1.getBonus();});
        System.out.println("xiyou平均"+SumSalary/(xiyou.size()-2));
        //4.共平均工资,去掉高低
        Stream<Employee> s1 = xiyou.stream();
        Stream<Employee> s2 = wangzhe.stream();
        Stream<Employee> s3 = Stream.concat(s1,s2);
        s3.sorted(((o1, o2) -> Double.compare(o1.getSalary()+o1.getBonus(), o2.getSalary()+o2.getBonus())))
                .skip(1).limit(xiyou.size() + wangzhe.size()-2)
                .forEach(e1 -> {SumSalary2+=e1.getSalary()+e1.getBonus();});
        BigDecimal a = BigDecimal.valueOf(SumSalary2);
        BigDecimal b = BigDecimal.valueOf(xiyou.size()+wangzhe.size()-2);

        System.out.println("xiyou平均"+a.divide(b,3, RoundingMode.HALF_UP));


    }
}

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 + '\'' +
                '}';
    }
}

class Niubier{
    private String name;
    private double salary;


    public Niubier(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    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 "Niubier{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值