Java8中的收集器

版权声明:随意转载,请勿商用。转载请注明出处,谢谢。 https://blog.csdn.net/xinhongyang/article/details/86632610

归约和汇总

计数

menu.stream().collect(Collectors.counting());
menu.stream().count();

最大值和最小值

Comparator<Dish> dishCaloriesComparator =Comparator.comparingInt(Dish::getCalories);
Optional<Dish> mostCalorieDish =menu.stream().collect(maxBy(dishCaloriesComparator));

汇总

  1. 求和
int totalCalories = menu.stream().collect(summingInt(Dish::getCalories));
  1. 求平均数
double avgCalories = menu.stream().collect(averagingInt(Dish::getCalories));
  1. 求各种统计数据
IntSummaryStatistics menuStatistics = menu.stream().collect(summarizingInt(Dish::getCalories));
//结果为:IntSummaryStatistics{count=9, sum=4300, min=120,average=477.777778, max=800}
  1. 连接字符串
String shortMenu = menu.stream().map(Dish::getName).collect(joining());
String shortMenu = menu.stream().map(Dish::getName).collect(joining(", "));

广义的归约汇总

int totalCalories = menu.stream().collect(reducing(0, Dish::getCalories, (i, j) -> i + j));
Optional<Dish> mostCalorieDish = menu.stream().collect(reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2));

分组

  • 一级分组
Map<Dish.Type, List<Dish>> dishesByType = menu.stream().collect(groupingBy(Dish::getType));
  • 多级分组
//可以把一个内层groupingBy传递给外层groupingBy
Map<Dish.Type, Map<CaloricLevel, List<Dish>>> dishesByTypeCaloricLevel = menu.stream().collect(
	groupingBy(Dish::getType,
		groupingBy(dish -> {
				if (dish.getCalories() <= 400) return CaloricLevel.DIET;
				else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
				else return CaloricLevel.FAT;
			} 
		)
	)
);

按照子组收集数据

传递给第一个groupingBy的第二个收集器可以是任何类型,因此可以做收集数据用,例如:

//{MEAT=3, FISH=2, OTHER=4}
Map<Dish.Type, Long> typesCount = menu.stream().collect(groupingBy(Dish::getType, counting()));
//{FISH=Optional[salmon], OTHER=Optional[pizza], MEAT=Optional[pork]}
menu.stream().collect(groupingBy(Dish::getType,maxBy(comparingInt(Dish::getCalories))));
把收集器返回的结果转换为另一种类型,你可以使用Collectors.collectingAndThen方法返回收集器

分区

分区是分组的特殊情况:由一个谓词作为分类函数。

Map<Boolean, List<Dish>> partitionedMenu =menu.stream().collect(partitioningBy(Dish::isVegetarian));

Collectors的静态方法

1
在这里图片描述

收集器接口

/**
 * T是流中要收集的项目的泛型
 * A是累加器的类型,累加器是在收集过程中用于积累部分结果的对象
 * R是收集操作得到的对象(通常但是不一定是集合)的类型
**/
public interface Collector<T, A, R> {
	/**
	 * 返回一个结果为空的Supplier,在调用时它会创建一个空的累加器实例,供数据收集过程使用。
	 **/
	Supplier<A> supplier();
	/**
	 * 返回执行归约操作的函数,当遍历到流中的第n个元素时,这个函数执行时会有两个参数:保存归约结果的累加器(收集了流中的前n-1个项目),还有第n个元素本身。该函数将返回void。
	 **/
	BiConsumer<A, T> accumulator();
	/**
	 * 将累加器对象转换为整个集合操作的最终结果。
	 **/
	Function<A, R> finisher();
	/**
	 * combiner方法会返回一个供归约操作使用的函数,它定义了对流的各个子部分进行并行处理时,各个子部分归约所得的累加器要如何合并。
	 **/
	BinaryOperator<A> combiner();
	/**
	 * 返回一个不可变的Characteristics集合,定义了收集器的行为,尤其是关于流是否可以并行归约,以及可以使用哪些优化的提示。
	 **/
	Set<Characteristics> characteristics();
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xinhongyang/article/details/86632610