collectors 求和_Collectors工具类

本文详细介绍了Java中Collectors工具类的各种方法,包括joining、toCollection、toList、toSet、toMap、reducing、averaging、counting、maxBy、minBy、summing、summarizing等,通过实例展示了它们的使用方法,帮助理解如何进行数据聚合和处理。
摘要由CSDN通过智能技术生成

public classCollectors_example {public static voidmain(String[] args) {

List appleList = new ArrayList<>();

appleList.add(new Apple("red", 170));

appleList.add(new Apple("green", 150));

appleList.add(null);

appleList.add(new Apple("yellow", 170));

appleList.add(new Apple("green", 100));

appleList.add(new Apple("red", 170));//joining: 拼接

String join = appleList.stream().map(x ->x.getColor()).collect(Collectors.joining());

String join1= appleList.stream().map(x -> x.getColor()).collect(Collectors.joining("-"));

String join2= appleList.stream().map(x -> x.getColor()).collect(Collectors.joining("-", "前缀", "后缀"));

System.out.println(join2);//toCollection、toList与toSet

ArrayList arrayList = appleList.stream().map(x -> x.getWeight()).collect(Collectors.toCollection(ArrayList::new));

List arrayList1 = appleList.stream().map(x ->x.getWeight()).collect(Collectors.toList());

Set hashSet = appleList.stream().map(x ->x.getWeight()).collect(Collectors.toSet());

System.out.println("----------------------------------");//toMap与toConcurrentMap: 键不能重复!!! toConcurrentMap同toMap

Map map4 =appleList.stream().collect(Collectors.toMap(Apple::getColor, Apple::getWeight));

Map map5 = appleList.stream().collect(Collectors.toMap(Apple::getWeight, x -> 1, Integer::sum));

Hashtable hashtable = appleList.stream().collect(Collectors.toMap(Apple::getWeight, x -> 1, Integer::sum, Hashtable::new));

System.out.println(map5);

System.out.println(hashtable);//reducing: 对输入元素执行汇聚操作

Apple apple = appleList.stream().collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparingInt(Apple::getWeight)))).orElse(null);

Integer inte= appleList.stream().map(x -> x.getWeight()).collect(Collectors.reducing(10000, Integer::sum));

Integer inte1= appleList.stream().collect(Collectors.reducing(0, Apple::getWeight, Integer::sum));

System.out.println(inte1);/******************求值: list中如果存在空元素会抛空指针异常!!******************/

//averaging: 平均值//averagingInt/averagingLong/averagingDouble

Double collect =appleList.stream().collect(Collectors.averagingDouble(Apple::getWeight));

System.out.println(collect);//counting: 统计个数

Long count =appleList.stream().collect(Collectors.counting());

Long emptyCount=Stream.empty().collect(Collectors.counting());//Stream.count()方法的返回值是long型,counting()方法返回值是Collector类型

long streamCount =appleList.stream().count();

System.out.println(count);

System.out.println(emptyCount);//maxBy、minBy: 最大最小值

appleList.stream().collect(Collectors.maxBy(Comparator.comparingInt(Apple::getWeight))).ifPresent(System.out::println);

appleList.stream().collect(Collectors.minBy(Comparator.comparingInt(Apple::getWeight))).ifPresent(System.out::println);//summing、summarizing: 求和

Integer sum1 =appleList.stream().collect(Collectors.summingInt(Apple::getWeight));

IntSummaryStatistics iss=appleList.stream().collect(Collectors.summarizingInt(Apple::getWeight));

System.out.println(iss.getMax());

System.out.println(iss.getMin());

System.out.println(iss.getCount());

System.out.println(iss.getSum());

System.out.println(iss.getAverage());/******************分组******************/

//groupingBy: 分组

Map> map =appleList.stream().collect(Collectors.groupingBy(Apple::getColor));

Map longMap =appleList.stream().collect(Collectors.groupingBy(Apple::getColor, Collectors.counting()));

TreeMap treeMap = appleList.stream().collect(Collectors.groupingBy(Apple::getColor, TreeMap::new, Collectors.counting()));

longMap.forEach((k, v)->{

System.out.println(k+ ":" +v);

});//groupingByConcurrent: 操作同groupingBy; 元素整理成ConcurrentMap(线程安全)

ConcurrentMap> map1 =appleList.stream().collect(Collectors.groupingByConcurrent(Apple::getColor));//partitioningBy: 分区;例:大于150的一个组,小于150的一个组

Map> map2 = appleList.stream().collect(Collectors.partitioningBy(x -> x.getWeight() > 150));

Map map3 = appleList.stream().collect(Collectors.partitioningBy(x -> x.getWeight() > 150, Collectors.counting()));

map3.forEach((k, v)->{

System.out.println(k+ ":" +v);

});//collectingAndThen: 收集后操作

String avg = appleList.stream().collect(Collectors.collectingAndThen(Collectors.averagingInt(Apple::getWeight), item -> "average weight is " +item));

System.out.println(avg);//mapping: 在调用mapper之后,将调用结果的返回值作为downstream的输入元素,再调用downstream

String collect1 = appleList.stream().collect(Collectors.mapping(Apple::getColor, Collectors.joining("-")));

System.out.println(collect1);

}/******************自定义******************/

public static Collector, Set>toImmutableSet() {return Collector.of(HashSet::new, Set::add, (left, right) ->{

left.addAll(right);returnleft;

}, t->t, Collector.Characteristics.IDENTITY_FINISH);

}public static > Collector> toImmutableSet(Suppliersupplier) {returnCollector.of(

supplier,

Set::add, (left, right)->{

left.addAll(right);returnleft;

}, Collections::unmodifiableSet);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值