JAVA学习-笔记04-Collectors

Collectors详解

1.averagingDouble:方法返回一个Collector收集器,它生成应用于输入元素的double值函数的算术平均值。如果没有元素,则结果为0。
Double averagingDouble = menu.stream().collect(Collectors.averagingDouble(Student2::getTotalScore));
System.out.println(averagingDouble);
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Double collect = integerStream.collect(Collectors.averagingInt(Integer::valueOf));
Double collect1 = integerStream.collect(Collectors.averagingDouble(Double::valueOf));
Double collect2 = integerStream.collect(Collectors.averagingLong(Long::valueOf));

2.collectingAndThen:方法调整Collector收集器以执行其它的结束转换。
List<Student> studentList = menu.stream().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
System.out.println(studentList);
String collect = menu.stream().collect(Collectors.collectingAndThen(Collectors.averagingInt(Student::getTotalScore), a -> "===" + a));
System.out.println(collect);

3.counting:方法返回一个Collector收集器接受T类型的元素,用于计算输入元素的数量。如果没有元素,则结果为0。
Optional.of(menu.stream().collect(Collectors.counting())).ifPresent(System.out::println);

4.groupingBy
groupingBy(Function):方法返回一个Collector收集器对T类型的输入元素执行"group by"操作,根据分类函数对元素进行分组,并将结果返回到Map。
Map<Student.GradeType, List<Student>> collect1 = menu.stream().collect(Collectors.groupingBy(Student::getGradeType));
Optional.ofNullable(collect1).ifPresent(System.out::println);

groupingBy(Function, Collector):方法返回一个Collector收集器,对T类型的输入元素执行级联"group by"操作,根据分类函数对元素进行分组,然后使用指定的下游Collector收集器对与给定键关联的值执行缩减操作。
Optional.of(menu.stream().collect(Collectors.groupingBy(Student::getGradeType, Collectors.counting()))).ifPresent(System.out::println);

groupingBy(Function, Supplier, Collector)方法返回一个Collector收集器,对T类型的输入元素执行级联"group by"操作,根据分类函数对元素进行分组,然后使用指定的下游Collector收集器对与给定键关联的值执行缩减操作。
收集器生成的Map是使用提供的工厂函数创建的。
Map<Student.GradeType, Double> map = menu.stream().collect(Collectors.groupingBy(Student::getGradeType,TreeMap::new,Collectors.averagingInt(Student::getTotalScore)));
Optional.of(map.getClass()).ifPresent(System.out::println);
Optional.of(map).ifPresent(System.out::println);

5.groupingByConcurrent
groupingByConcurrent(Function)方法返回一个并发Collector收集器对T类型的输入元素执行"group by"操作,根据分类函数对元素进行分组。
groupingByConcurrent(Function, Collector)方法返回一个并发Collector收集器,对T类型的输入元素执行级联"group by"操作,根据分类函数对元素进行分组,然后使用指定的下游Collector收集器对与给定键关联的值执行缩减操作。
groupingByConcurrent(Function, Supplier, Collector)方法返回一个并行Collector收集器,对T类型的输入元素执行级联"group by"操作,根据分类函数对元素进行分组,然后使用指定的下游Collector收集器对与给定键关联的值执行缩减操作。收集器生成的ConcurrentMap是使用提供的工厂函数创建的。

6.joining
joining()方法返回一个Collector收集器,它按遇见顺序将输入元素连接成String。
Optional.of(menu.stream().map(Student::getName).collect(Collectors.joining())).ifPresent(System.out::println);

joining(delimiter)方法返回一个Collector收集器,它以遇见顺序连接由指定分隔符分隔的输入元素。
Optional.of(menu.stream().map(Student::getName).collect(Collectors.joining(","))).ifPresent(System.out::println);

joining(delimiter, prefix, suffix)方法返回一个Collector收集器,它以遇见顺序将由指定分隔符分隔的输入元素与指定的前缀和后缀连接起来。
Optional.of(menu.stream().map(Student::getName).collect(Collectors.joining(",", "Names[", "]"))).ifPresent(System.out::println);

7.mapping:方法通过在累积之前将映射函数应用于每个输入元素,将Collector收集器接受U类型的元素调整为一个接受T类型的元素。
Optional.of(menu.stream().collect(Collectors.mapping(Student::getName, Collectors.joining(",")))).ifPresent(System.out::println);

8.maxBy:方法返回一个Collector收集器,它根据给定的Comparator比较器生成最大元素,描述为Optional。
menu.stream().collect(Collectors.maxBy(Comparator.comparingInt(Student::getTotalScore))).ifPresent(System.out::println);

9.minBy:方法返回一个Collector收集器,它根据给定的Comparator比较器生成最小元素,描述为Optional。
menu.stream().collect(Collectors.minBy(Comparator.comparingInt(Student::getTotalScore))).ifPresent(System.out::println);

10.partitioningBy
partitioningBy(Predicate)方法返回一个Collector收集器,它根据Predicate对输入元素进行分区,并将它们组织成Map。
Map<Boolean, List<Student>> collect = menu.stream().collect(Collectors.partitioningBy(Student::isLocal));
Optional.of(collect).ifPresent(System.out::println);

partitioningBy(Predicate, Collector)方法返回一个Collector收集器,它根据Predicate对输入元素进行分区,根据另一个Collector收集器减少每个分区中的值,并将它们组织成Map,其值是下游减少的结果。
Map<Boolean, Double> collect = menu.stream().collect(Collectors.partitioningBy(Student::isLocal,Collectors.averagingInt(Student::getTotalScore)));
Optional.of(collect).ifPresent(System.out::println);

11.reducing
reducing(BinaryOperator)返回一个Collector收集器,它在指定的BinaryOperator下执行其输入元素的缩减。结果被描述为Optional
menu.stream().collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparingInt(Student::getTotalScore)))).ifPresent(System.out::println);

reducing(Object, BinaryOperator)返回一个Collector收集器,它使用提供的标识在指定的BinaryOperator下执行其输入元素的缩减。
Integer result = menu.stream().map(Student::getTotalScore).collect(Collectors.reducing(0, (d1, d2) -> d1 + d2));
System.out.println(result);

reducing(Object, Function, BinaryOperator)返回一个Collector收集器,它在指定的映射函数和BinaryOperator下执行其输入元素的缩减。这是对reducing(Object, BinaryOperator)的概括,它允许在缩减之前转换元素。
Integer result = menu.stream().collect(Collectors.reducing(0, Student::getTotalScore, (d1, d2) -> d1 + d2));
System.out.println(result);

12.summarizingDouble:方法返回一个Collector收集器,它将double生成映射函数应用于每个输入元素,并返回结果值的摘要统计信息。
DoubleSummaryStatistics result = menu.stream().collect(Collectors.summarizingDouble(Student::getTotalScore));
Optional.of(result).ifPresent(System.out::println);

13.summingDouble:返回一个Collector收集器,它生成应用于输入元素的double值函数的总和。如果没有元素,则结果为0。
Optional.of(menu.stream().collect(Collectors.summingDouble(Student::getTotalScore))).ifPresent(System.out::println);

14.toCollection:返回一个Collector收集器,它按遇见顺序将输入元素累积到一个新的Collection收集器中。Collection收集器由提供的工厂创建。
Optional.of(menu.stream().filter(d -> d.getTotalScore() > 600).collect(Collectors.toCollection(LinkedList::new))).ifPresent(System.out::println);

15.toConcurrentMap
toConcurrentMap(Function, Function)返回一个并发的Collector收集器,它将元素累积到ConcurrentMap中,其键和值是将提供的映射函数应用于输入元素的结果。
Optional.of(menu.stream().collect(Collectors.toConcurrentMap(Student::getName, Student::getTotalScore))).ifPresent(System.out::println);

toConcurrentMap(Function, Function, BinaryOperator)返回一个并发的Collector收集器,它将元素累积到ConcurrentMap中,其键和值是将提供的映射函数应用于输入元素的结果。
Optional.of(menu.stream().collect(Collectors.toConcurrentMap(Student::getGradeType, v -> 1L, (a, b) -> a + b))).ifPresent(System.out::println);

toConcurrentMap(Function, Function, BinaryOperator, Supplier)返回一个并发的Collector收集器,它将元素累积到ConcurrentMap中,其键和值是将提供的映射函数应用于输入元素的结果。
Optional.of(menu.stream().collect(Collectors.toConcurrentMap(Student::getGradeType, v -> 1L, (a, b) -> a + b, ConcurrentSkipListMap::new))).ifPresent(System.out::println);
        
16.toList:返回一个Collector收集器,它将输入元素累积到一个新的List中。返回的List的类型,可变性,可序列化或线程安全性无法保证;如果需要更多地控制返回的List,请使用toCollection(Supplier)。
Optional.of(menu.stream().filter(Student::isLocal).collect(Collectors.toList())).ifPresent(System.out::println);
        
17.toMap
toMap(Function, Function)返回一个Collector收集器,它将元素累积到Map中,其键和值是将提供的映射函数应用于输入元素的结果。
toMap(Function, Function, BinaryOperator)返回一个并发的Collector收集器,它将元素累积到Map中,其键和值是将提供的映射函数应用于输入元素的结果。
toMap(Function, Function, BinaryOperator, Supplier)返回一个并发的Collector收集器,它将元素累积到Map中,其键和值是将提供的映射函数应用于输入元素的结果。
array_column(二维数组,null,"xxx") ==》 二维数组.stream().collect(Collectors.toMap(ShopGoodsSkuDto::getXXX, Function.identity()));
array_column(二维数组,"yyy","xxx") ==》二维数组.stream().collect(Collectors.toMap(ShopGoodsSkuDto::getXXX, ShopGoodsSkuDto::getYYY));

18.toSet:返回一个Collector收集器,它将输入元素累积到一个新的Set中。返回的Set的类型,可变性,可序列化或线程安全性无法保证;如果需要更多地控制返回的Set,请使用toCollection(Supplier)。
Optional.of(menu.stream().filter(Student2::isLocal).collect(Collectors.toSet())).ifPresent(System.out::println);


测试demo
import lombok.Data;

@Data
public class Student {
    /** 姓名 */
    private String name;
    /** 总分 */
    private int totalScore;
    /** 是否本地人 */
    private boolean local;
    /** 年级 */
    private GradeType gradeType;
    /** 年级类型 */
    public enum GradeType {ONE,TWO,THREE}
    public Student2(String name, int totalScore, boolean local, GradeType gradeType) {
        this.name = name;
        this.totalScore = totalScore;
        this.local = local;
        this.gradeType = gradeType;
    }
    @Override
    public String toString() {
        return "Student{"+"name='"+name+'\''+", totalScore="+totalScore+", local="+local+", gradeType="+gradeType+'}';
    }
}
测试demo参数
List<Student> menu = Arrays.asList(
    new Student("刘一", 721, true, Student.GradeType.THREE),
    new Student("陈二", 637, true, Student.GradeType.THREE),
    new Student("张三", 666, true, Student.GradeType.THREE),
    new Student("李四", 531, true, Student.GradeType.TWO),
    new Student("王五", 483, false, Student.GradeType.THREE),
    new Student("赵六", 367, true, Student.GradeType.THREE),
    new Student("孙七", 499, false, Student.GradeType.ONE)
);

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值