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);
}
}
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230107.html原文链接:https://javaforall.net
