[每日知识点] Collectors 类API学习

Collectors 类常用API学习
averagingDouble/averagingInt/averagingLong 根据不同类型求平均值
counting() 统计数量
groupingBy()按某个规则分组
summarizingDouble/summarizingInt/summarizingLong 返回一个 Collector 
summingLong/summingInt/summingDouble 求和 
toMap()
toList() /toSet    

 

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

/**
 * @Description: Collectors 类API学习
 * @Date: 2019/8/8
 * @Version: V1.0
 */
public class TestCollectors {
    public static void main(String[] args) {
     List personList = Arrays.asList(new Person("wzn",18),
             new Person("w",19),
             new Person("z",20),
             new Person("n",18),
             new Person("wzn",21));
        //averagingDouble/averagingInt/averagingLong 根据不同类型求平均值
        Double age = personList.stream().collect(Collectors.averagingInt(Person::getAge));
        System.out.println("平均值:"+age);

        System.out.println("========================================");
        //counting() 计数 单独使用没有意义
        Long count = personList.stream().collect(Collectors.counting());
        System.out.println("计数:"+count);
        System.out.println("========================================");

        //groupingBy()按某个规则分组
        Map> groupMap = personList.stream().collect(Collectors.groupingBy(Person::getName));
        System.out.println(JSON.toJSON(groupMap).toString());
        System.out.println("========================================");

        //groupingBy利用分组功能和counting 统计重复数据出现次数
        Map groupCountMap = personList.stream().collect(Collectors.groupingBy(Person::getName, Collectors.counting()));
        System.out.println("重复数据统计:"+groupCountMap);
        System.out.println("========================================");

        //summarizingDouble/summarizingInt/summarizingLong 返回一个 Collector 求平均值 大小值等
        IntSummaryStatistics summaryStatistics = personList.stream().collect(Collectors.summarizingInt(Person::getAge));
        System.out.println("person中 age最小值:"+summaryStatistics.getMin());
        System.out.println("========================================");

        //summingLong/summingInt/summingDouble 求和
        Integer sum = personList.stream().collect(Collectors.summingInt(Person::getAge));
        System.out.println("年龄之和:"+sum);
        //另外一种求和写法
        int sum1 = personList.stream().mapToInt(Person::getAge).sum();
        System.out.println("求和"+sum1);
        System.out.println("========================================");

        //toList() /toSet 过滤大于18 的人 生成新的数组 sorted()默认方法使用必须在实体bean中重写排序方法
        List filterList = personList.stream().filter(s -> s.getAge() > 18).sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
        List filterList1 = personList.stream().filter(s -> s.getAge() > 18).sorted(Comparator.comparing(Person::getAge).reversed()).collect(Collectors.toList());
        System.out.println("排序正序:"+JSON.toJSON(filterList));
        System.out.println("排序倒序:"+JSON.toJSON(filterList1));
        System.out.println("========================================");

        //toMAP()
        Map testMap = new HashMap<>();
        testMap.put("wzn",19);
        testMap.put("w",1);
        testMap.put("z",2);
        LinkedHashMap linkedHashMapKey = testMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
        LinkedHashMap linkedHashMapValue = testMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
        System.out.println("按照key排序:"+JSON.toJSON(linkedHashMapKey).toString());
        System.out.println("按照value排序:"+JSON.toJSON(linkedHashMapValue).toString());

    }

    @Setter
    @Getter
   static class Person{
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
}

[每日知识点] Collectors 类API学习_第1张图片

你可能感兴趣的:(java基础,Lambda)