Skip to content

Collectors使用示例

yangyp8110 edited this page Jan 17, 2018 · 1 revision
  • demo实体:
    public class Employee{
        private int empId;
        private String empName;
        private int salary;
        private int status;
        private boolean manage;

        public int getEmpId() { return empId; }
        public void setEmpId(int empId) { this.empId = empId; }
        public String getEmpName() { return empName; }
        public void setEmpName(String empName) { this.empName = empName; }
        public int getSalary() { return salary; }
        public void setSalary(int salary) { this.salary = salary; }
        public int getStatus() { return status; }
        public void setStatus(int status) { this.status = status; }
        public boolean getManage() { return manage; }
        public void setManage(boolean manage) { this.manage = manage; }
    }
  • 测试示例
    public static void main(String[] args){
        List<Employee> list = new ArrayList<Employee>(){
            {
                add(new Employee(){{setEmpId(111);setEmpName("emp 111");setSalary(10000);setStatus(1);setManage(false);}});
                add(new Employee(){{setEmpId(222);setEmpName("emp 222");setSalary(11000);setStatus(3);setManage(false);}});
                add(new Employee(){{setEmpId(333);setEmpName("emp 333");setSalary(20000);setStatus(2);setManage(true);}});
                add(new Employee(){{setEmpId(444);setEmpName("emp 444");setSalary(11800);setStatus(1);setManage(false);}});
                add(new Employee(){{setEmpId(555);setEmpName("emp 555");setSalary(16000);setStatus(2);setManage(true);}});
                add(new Employee(){{setEmpId(666);setEmpName("emp 666");setSalary(15000);setStatus(3);setManage(false);}});
            }
        };

        //方法 返回类型 作用
        //toList List<T> 把流中元素收集到List
        //ArrayList
        List<Employee> empsList= list.stream().collect(Collectors.toList());

        //toSet Set<T> 把流中元素收集到Set
        //HashSet
        Set<Employee> empsSet= list.stream().collect(Collectors.toSet());

        //toCollection Collection<T> 把流中元素收集到创建的集合
        //ArrayList
        Collection<Employee> empsArray=list.stream().collect(Collectors.toCollection(ArrayList::new));

        //counting Long 计算流中元素的个数
        //6
        long count = list.stream().collect(Collectors.counting());

        //summingInt Integer 对流中元素的整数属性求和
        int total = list.stream().collect(Collectors.summingInt(Employee::getSalary));

        //averagingInt Double 计算流中元素Integer属性的平均值
        double avg = list.stream().collect(Collectors.averagingInt(Employee::getSalary));

        //summarizingInt IntSummaryStatistics 收集流中Integer属性的统计值。如:平均值
        //IntSummaryStatistics{count=6, sum=83800, min=10000, average=13966.666667, max=20000}
        IntSummaryStatistics iss = list.stream().collect(Collectors.summarizingInt(Employee::getSalary));

        //joining String 连接流中每个字符串
        //emp 111emp 222emp 333emp 444emp 555emp 666
        String str = list.stream().map(Employee::getEmpName).collect(Collectors.joining());

        //maxBy Optional<T> 根据比较器选择最大值
        Optional<Employee> max = list.stream().collect(Collectors.maxBy(comparingInt(Employee::getSalary)));

        //minBy Optional<T> 根据比较器选择最小值
        Optional<Employee> min = list.stream().collect(Collectors.minBy(comparingInt(Employee::getSalary)));

        //reducing 归约产生的类型 从一个作为累加器的初始值开始,利用BinaryOperator与流中元素逐个结合,从而归约成单个值
        //83800
        int total111 = list.stream().collect(Collectors.reducing(0, Employee::getSalary, Integer::sum));

        //collectingAndThen 转换函数返回的类型 包裹另一个收集器,对其结果转换函数
        int how = list.stream().collect(Collectors.collectingAndThen(Collectors.toList(), List::size));

        //groupingBy Map<K, List<T>> 根据某属性值对流分组,属性为K,结果为V
        Map<Integer, List<Employee>> map = list.stream().collect(Collectors.groupingBy(Employee::getStatus));

        //partitioningBy Map<Boolean, List<T>> 根据true或false进行分区
        Map<Boolean,List<Employee>> vd = list.stream().collect(Collectors.partitioningBy(Employee::getManage));
        
        //distinct
        List<Integer> empIds = list.stream().map(x -> x.getEmpId()).distinct().collect(Collectors.toList())
    }
Clone this wiki locally