Stream流中的 max()和 sorted()方法

  • 需求:某个公司的开发部门,分为开发 一部 和 二部 ,现在需要进行年中数据结算。分析:
  1. 员工信息至少包含了(名称、性别、工资、奖金、处罚记录)
  2. 开发一部有 4 个员工、开发二部有 5 名员工
  3. 分别筛选出 2 个部门的最高工资的员工信息,封装成优秀员工对象Topperformer
  4. 分别统计出 2 个部门的平均月收入,要求去掉最高和最低工资
  5. 统计 2 个开发部门整体的平均工资,去掉最低和最高工资的平均值
  • 根据提供的Comparator返回此流的 最大元素
  • 返回由此流的元素组成的流,根据Comparator进行排序
package com.csdn.streampractice;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class StreamDemo04 {

    private static double allMoney; //开发一部去掉最高工资,最低工资的总和
    private static double allMoney2;// 2个部门去掉最高工资,最低工资的总和
    private static double averageTwo;//开发二部去掉最高工资,最低工资的总和

    public static void main(String[] args) {
        //开发一部
        List<Employee> one = new ArrayList<>();
        Collections.addAll(one, new Employee("张三丰", '男', 30000, 25000, null),
                new Employee("张翠山", '男', 25000, 1000, "顶撞上司"),
                new Employee("张无忌", '男', 20000, 20000, null),
                new Employee("赵敏", '女', 20000, 25000, null));

        //1.筛选出开发一部的最高工资的员工信息
//        Optional<Employee> optional= one.stream().max(new Comparator<Employee>() {
//            @Override
//            public int compare(Employee o1, Employee o2) {
//                return Double.compare(o1.getSalary() + o1.getBonus(), o2.getSalary() + o2.getBonus());
//            }
//        });
//        Employee employee = optional.get();
//        System.out.println(employee);

//        Optional<Employee> optional = one.stream().max((o1, o2) -> Double.compare(o1.getSalary() + o1.getBonus(), o2.getSalary() + o2.getBonus()));
//        Employee employee = optional.get();
//        System.out.println(employee);

        Optional<Employee> optional = one.stream().max(Comparator.comparingDouble(o -> o.getSalary() + o.getBonus()));
        Employee employee = optional.get();
        System.out.println(employee);//Employee(name=张三丰, sex=男, salary=30000.0, bonus=25000.0, punish=null)

        //2.封装成优秀员工对象TopperFormer
        Optional<TopperFormer> topperFormer = optional.map(e -> new TopperFormer(e.getName(), e.getSalary() + e.getBonus()));
        TopperFormer topperFormer1 = topperFormer.get();
        System.out.println(topperFormer1);//TopperFormer(name=张三丰, money=55000.0)

        //3.统计平均工资,去掉最高工资和最低工资
        one.stream().sorted(Comparator.comparingDouble(o -> o.getSalary() + o.getBonus())).skip(1).
                limit(one.size() - 2).forEach(new Consumer<Employee>() {
                    @Override
                    public void accept(Employee employee) {
                        allMoney += (employee.getSalary() + employee.getBonus());
                    }
                });
        System.out.println("开发一部的平均工资是:" + allMoney / (one.size() - 2));//开发一部的平均工资是:42500.0


        //开发二部
        List<Employee> two = new ArrayList<>();
        Collections.addAll(two, new Employee("喜羊羊", '男', 15000, 9000, null),
                new Employee("美羊羊", '女', 20000, 10000, null),
                new Employee("懒洋洋", '女', 50000, 100000, "被打"),
                new Employee("灰太狼", '男', 3500, 1000, "被打"),
                new Employee("红太狼", '女', 20000, 0, "下毒"));

        //1.筛选出开发二部的最高工资的员工信息
        Optional<Employee> optional2 = two.stream().max(Comparator.comparingDouble(o -> o.getSalary() + o.getBonus()));
        Employee employee2 = optional2.get();
        System.out.println(employee2);//Employee(name=懒洋洋, sex=女, salary=50000.0, bonus=100000.0, punish=被打)

        //2.封装成优秀员工对象TopperFormer
        Optional<TopperFormer> topperFormer2 = optional2.map(e -> new TopperFormer(e.getName(), e.getSalary() + e.getBonus()));
        TopperFormer topperFormer3 = topperFormer2.get();
        System.out.println(topperFormer3);//TopperFormer(name=懒洋洋, money=150000.0)

        //3.统计平均工资,去掉最高工资和最低工资
        two.stream().sorted(Comparator.comparingDouble(o -> o.getSalary() + o.getBonus())).skip(1).
                limit(two.size()-2).forEach(new Consumer<Employee>() {
                    @Override
                    public void accept(Employee employee) {
                        averageTwo += (employee.getSalary() + employee.getBonus());
                    }
                });
        System.out.println("开发二部的平均工资是:" + averageTwo / (two.size() - 2));//开发二部的平均工资是:24666.666666666668


        //4.统计 2 个开发部门整体的平均工资,去掉最低和最高工资的平均值
        Stream<Employee> s1 = one.stream();
        Stream<Employee> s2 = two.stream();
        Stream<Employee> s3 = Stream.concat(s1, s2);
        s3.sorted(Comparator.comparingDouble(o -> o.getSalary() + o.getBonus())).
                skip(1).limit(one.size() + two.size() - 2).forEach(employee1 -> {
                    allMoney2 += (employee1.getSalary() + employee1.getBonus());
                });
        //BigDecimal
        BigDecimal a = BigDecimal.valueOf(allMoney2);
        BigDecimal b = BigDecimal.valueOf(one.size() + two.size() - 2);
        System.out.println("开发部的平均工资是:" + a.divide(b,2, RoundingMode.HALF_UP));//开发部的平均工资是:34285.71
    }

}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Employee {
    private String name;
    private char sex;
    private double salary;
    private double bonus;
    private String punish;//处罚信息
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class TopperFormer {
    private String name;
    private double money;
}
package com.csdn.Stream2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class StreamSorted {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();

        Collections.addAll(list, "eee", "dd", "ccc", "bb", "aaa");

        //按照字母顺序把数据在控制台输出
        list.stream().sorted().forEach(System.out::println);
//                                                                    aaa
//                                                                    bb
//                                                                    ccc
//                                                                    dd
//                                                                    eee

        System.out.println("============");
        //按照字符串长度升序排序,如果长度相等,在按照字母顺序排序
        list.stream().sorted(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //按照字符串长度排序
                int num = o1.length() - o2.length();
                //如果字符串长度相等,按照字母顺序排序,如果字符串长度不相等,按照字符串长度升序排序
                int num2 = num == 0 ? o1.compareTo(o2) : num;
                return num2;
            }
        }).forEach(System.out::println);
//                                                                   bb
//                                                                   dd
//                                                                   aaa
//                                                                   ccc
//                                                                   eee


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值