首页 > 其他分享 >CollectorStream

CollectorStream

时间:2024-09-14 14:54:21浏览次数:8  
标签:System emps Emp println CollectorStream public out


package com.shrimpking.t4;

import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/9/11 20:15
 */
public class Emp
{
    private Integer no; //编号
    private String name; //姓名
    private Integer age; //年龄
    private String job; //职位
    private Integer salary; //工资
    private Integer bonus; //奖金
    private String dept; //部门

    public Emp(Integer no, String name, Integer age, String job, Integer salary, Integer bonus, String dept)
    {
        this.no = no;
        this.name = name;
        this.age = age;
        this.job = job;
        this.salary = salary;
        this.bonus = bonus;
        this.dept = dept;
    }

    public Integer getNo()
    {
        return no;
    }

    public void setNo(Integer no)
    {
        this.no = no;
    }

    public String getName()
    {
        return name;
    }

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

    public Integer getAge()
    {
        return age;
    }

    public void setAge(Integer age)
    {
        this.age = age;
    }

    public String getJob()
    {
        return job;
    }

    public void setJob(String job)
    {
        this.job = job;
    }

    public Integer getSalary()
    {
        return salary;
    }

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

    public Integer getBonus()
    {
        return bonus;
    }

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

    public String getDept()
    {
        return dept;
    }

    public void setDept(String dept)
    {
        this.dept = dept;
    }

    @Override
    public String toString()
    {
        return "Emp{" + "no=" + no + ", name='" + name + '\'' + ", age=" + age + ", job='" + job + '\'' + ", salary=" + salary + ", bonus=" + bonus + ", dept='" + dept + '\'' + '}';
    }

    @Override
    public boolean equals(Object o)
    {
        Emp emp = (Emp)o;
        return this.no.equals(emp.no);
    }

    @Override
    public int hashCode()
    {
        return this.no;
    }
}
package com.shrimpking.t4;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/9/11 20:17
 */
public class EmpFactory
{
    private static Emp[] emps = new Emp[]{
            new Emp(7369,"SMITH",28,"CLERK",3000,null,"RESEARCH"),
            new Emp(7499,"ALLEN",24,"SALESMAN",4000,300,"SALES"),
            new Emp(7521,"WARD",30,"SALESMAN",2500,500,"SALES"),
            new Emp(7566,"JONES",32,"MANAGER",5000,null,"RESEARCH"),
            new Emp(7654,"MARTIN",27,"SALESMAN",2800,1400,"SALES"),
            new Emp(7698,"BLAKE",35,"MANAGER",4500,null,"ACCOUNTING"),
            new Emp(7782,"CLARK",29,"MANAGER",4300,null,"RESEARCH"),
            new Emp(7788,"SCOTT",38,"ANALYST",6000,null,"RESEARCH"),
            new Emp(7839,"KING",40,"PRESIDENT",8000,null,"ACCOUNTING")
    };

    public static Emp[] getEmps(){
        return emps;
    }
}
package com.shrimpking.t4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/9/11 20:25
 */
public class CollectorStream
{
    public static void main(String[] args)
    {
        Emp[] emps = EmpFactory.getEmps();

        //将流中元素收集到list中
        System.out.println("List");
        List<Emp> list = Arrays.stream(emps)
                .collect(Collectors.toList());
        list.forEach(e -> System.out.println(e));
        System.out.println();

        //将流中元素收集到Set中
        System.out.println("Set");
        Set<Emp> empSet = Arrays.stream(emps)
                .collect(Collectors.toSet());
        empSet.forEach(e -> System.out.println(e));
        System.out.println();


        //将流中元素收集到集合中
        System.out.println("Collection");
        Collection<Emp> empColl = Arrays.stream(emps)
                .collect(Collectors.toCollection(ArrayList::new));
        empColl.forEach(e -> System.out.println(e));
        System.out.println();

        //将流中元素收集到Map中,以员工姓名为key,员工对象为值
        //静态方法 Function.identity()表示当前Emp对象
        System.out.println("Map");
        Map<String,Emp> empMap = Arrays.stream(emps)
                .collect(Collectors.toMap(Emp::getName, Function.identity()));
        empMap.forEach((k,v) -> {
            System.out.print(k + " "+ v);
            System.out.println();
        });
        System.out.println();

        //
        System.out.println("Avg");
        double avgSalary = Arrays.stream(emps)
                .collect(Collectors.averagingDouble(Emp::getSalary));
        System.out.println("所有员工工资的平均值=" + avgSalary);
        System.out.println();

        System.out.println("Sum");
        int sumSalary = Arrays.stream(emps)
                .collect(Collectors.summingInt(Emp::getSalary));
        System.out.println("所有员工工资的总和:" + sumSalary);
        System.out.println();

        //获取工资的最大值,
        System.out.println("Max");
        Optional<Integer> maxSalary = Arrays.stream(emps)
                .map(Emp::getSalary)
                .collect(Collectors.maxBy(Comparator.comparing(Function.identity())));
        System.out.println("员工中工资最高的是:" + maxSalary.get());
        System.out.println();

        //获取年龄的最小值
        System.out.println("Min");
        Optional<Integer> minAge = Arrays.stream(emps)
                .map(Emp::getAge)
                .collect(Collectors.minBy(Comparator.comparing(Function.identity())));
        System.out.println("员工中年龄最小的是:" + minAge.get());
        System.out.println();

        //收集工资的统计值
        System.out.println("Stat");
        IntSummaryStatistics statistics = Arrays.stream(emps)
                .collect(Collectors.summarizingInt(Emp::getSalary));
        System.out.println("工资:" + statistics);
        System.out.println();

        //收集员工姓名,逗号分隔
        System.out.println("Name");
        String names = Arrays.stream(emps)
                .map(Emp::getName)
                .collect(Collectors.joining(","));
        System.out.println("姓名:" + names);
        System.out.println();

        //获取有奖金的员工
        System.out.println("bonus");
        List<Emp> bonusList = Arrays.stream(emps)
                .filter(x -> x.getBonus() != null)
                .collect(Collectors.toList());
        for (Emp e : bonusList){
            System.out.printf("%s的奖金是%d\t\n",e.getName(),e.getBonus());
        }
        System.out.println();

        //根据员工部门进行分组
        System.out.println("group");
        Map<String,List<Emp>> groupList = Arrays.stream(emps)
                .collect(Collectors.groupingBy(Emp::getDept));
        Set<String> deptSet = groupList.keySet();
        for (String dept : deptSet){
            System.out.printf("[%s]部门的员工有:",dept);
            List<Emp> empsOfDept = groupList.get(dept);
            for (Emp emp : empsOfDept)
            {
                System.out.print(emp.getName() + " ");
            }
            System.out.println(); //换行
        }
        System.out.println();


        //根据员工是否有奖金,进行分区
        System.out.println("partition");
        Map<Boolean,List<Emp>> bonusEmps = Arrays.stream(emps)
                .collect(Collectors.partitioningBy(m -> m.getBonus() != null));
        Set<Boolean> isBonus = bonusEmps.keySet();
        for (Boolean bonus : isBonus)
        {
            if(bonus){
                System.out.println("有奖金的员工是:");
            }else{
                System.out.println("无奖金的员工是:");
            }

            List<Emp> empsOfBonus = bonusEmps.get(bonus);
            for (Emp emp : empsOfBonus)
            {
//                System.out.print(emp.getName() + ":" + emp.getBonus() + " ");
                System.out.printf("%s:%d ",emp.getName(),emp.getBonus());
            }
            System.out.println(); //换行
        }
        System.out.println();


    }
}

标签:System,emps,Emp,println,CollectorStream,public,out
From: https://blog.51cto.com/u_15356972/12016825

相关文章