java8 Collectors类的静态工厂方法

toList    List<T>

把流中所有项目收集到一个 List


Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c"});
List<String> list = stream.collect(Collectors.toList());

toSet   Set<T>

把流中所有项目收集到一个 Set,删除重复项

Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c", "a"});
Set<String> set = stream.collect(Collectors.toSet());

counting   Long

计算流中元素的个数

Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c", "a"});
long count = stream.collect(Collectors.counting());

summingInt   Integer 1

求和

Stream<Integer> stream = Arrays.stream(new Integer[]{1, 3, 5, 7});
int sum = stream.collect(Collectors.summingInt(a -> a));
System.out.println(sum);

averagingInt   Double

计算流中项目 Integer 属性的平均值

Stream<Integer> stream = Arrays.stream(new Integer[]{1, 3, 5, 7});
double avg = stream.collect(Collectors.averagingInt(a -> a));
System.out.println(avg);

summarizingInt   IntSummaryStatistics

收集关于流中项目 Integer 属性的统计值,例如最大、最小、 总和与平均值

 Stream<Integer> stream = Arrays.stream(new Integer[]{1, 3, 5, 7});
 IntSummaryStatistics statistics = stream.collect(Collectors.summarizingInt(a -> a));
 System.out.println("count:" +statistics.getCount() + ",sum:" + statistics.getSum() + ", min:" + statistics.getMin());

joining  String

连接对流中每个项目调用 toString 方法所生成的字符串

Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c"});
String join = stream.collect(Collectors.joining(","));
System.out.println(join);

maxBy   Optional<T>

一个包裹了流中按照给定比较器选出的最大元素的 Optional, 或如果流为空则为 Optional.empty()

        Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
        Optional<Integer> max = stream.collect(
                Collectors.maxBy(
                        Comparator.comparing(a -> a)));
        System.out.println(max.get());

minBy   Optional<T>

一个包裹了流中按照给定比较器选出的最小元素的 Optional,或如果流为空则为 Optional.empty()

        Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
        Optional<Integer> min = stream.collect(
                Collectors.minBy(
                        Comparator.comparing(a -> a)));
        System.out.println(min.get());

reducing   归约操作产生的类型

从一个作为累加器的初始值开始,利用 BinaryOperator 与流 中的元素逐个结合,从而将流归约为单个值

        Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
        int sum = stream.collect(
                Collectors.reducing(0, a -> a, Integer::sum));
        System.out.println(sum);

collectingAndThen   转换函数返回的类型

包裹另一个收集器,对其结果应用转换函数

        Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
        int count = stream.collect(
                Collectors.collectingAndThen(
                        Collectors.toList(), List::size));
        System.out.println(count);

groupingBy    Map<K, List<T>>

根据项目的一个属性的值对流中的项目作问组,并将属性值作为结果 Map 的键

 People p1 = new People("小明", "男", 11);
 People p2 = new People("小红", "女", 10);
 Stream<People> stream = Arrays.asList(p1, p2).stream();
 Map<String, List<People>> map = stream.collect(
                Collectors.groupingBy(People::getSex));


 public class People {
        private String name;
        private String sex;
        private int age;

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

        public String getName() {
            return name;
        }

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

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public int getAge() {
            return age;
        }

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

partitioningBy   Map<Boolean,List<T>>

根据对流中每个项目应用谓词的结果来对项目进行分区

        People p1 = new People("小明", "男", 11);
        People p2 = new People("小红", "女", 10);
        Stream<People> stream = Arrays.asList(p1, p2).stream();
        Map<Boolean, List<People>> map = stream.collect(
                Collectors.partitioningBy(p -> "男".equals(p.getSex())));

《中医之钥》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古智云开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值