利用JAVA流处理-统计男员工人数;找出所有薪资大于 5000 元的女员工;找出大于平均年龄的员工

利用JAVA流处理-统计男员工人数;找出所有薪资大于 5000 元的女员工;找出大于平均年龄的员工,第1张

创建一个Employee JAVABean对象

package ght04_Stream;
import java.util.ArrayList;
import java.util.List;

public class Employee { // 员工类
	private String name; // 姓名
	private int age; // 年龄
	private double salary; // 工资
	private String sex; // 性别
	private String dept; // 部门

	// 构造方法
	public Employee(String name, int age, double salary, String sex, String dept) {
		this.name = name;
		this.age = age;
		this.salary = salary;
		this.sex = sex;
		this.dept = dept;
	}

	// 重写此方法,方便打印输出员工信息
	public String toString() {
		return "name=" + name + ", age=" + age + ", salary=" + salary + ", sex=" + sex + ", dept=" + dept;
	}

	// 以下是员工属性的getter方法
	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public double getSalary() {
		return salary;
	}

	public String getSex() {
		return sex;
	}

	public String getDept() {
		return dept;
	}

	static List getEmpList() { // 提供数据初始化方法
		List list = new ArrayList();
		// 添加员工数据
		list.add(new Employee("老张", 40, 9000, "男", "运营部"));
		list.add(new Employee("小刘", 24, 5000, "女", "开发部"));
		list.add(new Employee("大刚", 32, 7500, "男", "销售部"));
		list.add(new Employee("翠花", 28, 5500, "女", "销售部"));
		list.add(new Employee("小马", 21, 3000, "男", "开发部"));
		list.add(new Employee("老王", 35, 6000, "女", "人事部"));
		list.add(new Employee("小王", 21, 3000, "女", "人事部"));
		return list;
	}
}

利用JAVA流处理Stream测试数据

package ght04_Stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class test {
    public static void main(String[] args) {
        List list = Employee.getEmpList();    //get test data from public class
        Stream stream = list.stream();    //get stream object

        // 分组规则方法,按照员工进行分级(sex)
        Function f = Employee::getSex;
        // 按照部门分成若干List集合,集合中保存员工对象,返回成Map
        Map> map = stream.collect(Collectors.groupingBy(f));
        Set keySet = map.keySet(); // 获取Map的 all sex

        int age = 0;    //年龄总和
        double avg_age = 0.0;
        for ( Employee e : list ){
             age += e.getAge();
        }
        avg_age = age/list.size();

        int num = 0;
        int salary = 5000;
        int s_num = 0;
        int a_num = 0;
        Listemployees = new ArrayList<>();

        for (String sex : keySet) { // 遍历部门名称集合
            // find sex
            if(sex == "男"){
                List deptList = map.get(sex); // 获取sex == "男"
                System.out.println("男员工人数为: "+deptList.size());
                for(Employee e : deptList){
                    if(e.getAge() > avg_age){
                        employees.add(e);
                    }
                }

            }else{
                List deptList = map.get(sex); // 获取sex == " 女"
                for(Employee d : deptList){
                    if(d.getSalary() > 5000){
                        s_num++;
                    }
                    if( d.getAge() > avg_age){
                        employees.add(d);
                    }
                }
                System.out.println("薪资大于5000的女员工人数为: "+s_num);
            }
        }
        System.out.println("大于平均年龄的员工: ");
        for (Employee emp : employees) { // 遍历员工集合
            System.out.println("\t" + emp); // 输出员工信息
        }

    }
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/741283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存