Collectors.reducing总结
1. 方法签名 一个参数
public static <T> Collector<T, ?, Optional<T>> reducing(BinaryOperator<T> op)
参数说明
- BinaryOperator op 归集操作函数 输入参数T返回T
测试代码
我们这里实现一个简单的求和功能,代码如下
@Testpublic void testReducingOne() {List<Integer> testData = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);Optional<Integer> sum = testData.stream().collect(Collectors.reducing((prev, cur) -> {System.out.println("prev=>" + prev + "cur=>" + cur);return prev + cur;}));System.out.print(sum.get()); // 45 }
2. 方法签名 两个参数
public static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op)
参数说明
- T identity 返回类型T初始值
- BinaryOperator op 归集操作函数 输入参数T返回T
测试代码
我们这里实现一个简单的求和并加上20功能,代码如下
@Testpublic void testReducingTwo() {List<Integer> testData = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);Integer sum = testData.stream().collect(Collectors.reducing(20, (prev, cur) -> {System.out.println("prev=>" + prev + "cur=>" + cur);return prev + cur;}));System.out.print(sum); //65}
2. 方法签名 三个参数
public static <T, U> Collector<T, ?, U> reducing(U identity,Function<? super T, ? extends U> mapper,BinaryOperator<U> op)
这个函数才是真正体现reducing(归集)的过程。调用者要明确知道以下三个点
- 需要转换类型的初始值
- 类型如何转换
- 如何收集返回值
参数说明
- U identity 最终返回类型U初始值
- Function<? super T, ? extends U> mapper 将输入参数T转换成返回类型U的函数
- BinaryOperator<U> op 归集操作函数 输入参数U返回U
测试代码
我们这里实现一个简单数字转字符串并按逗号连接的功能,代码如下
@Testpublic void testReducingThree() {List<Integer> testData = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);String joinStr = testData.stream().collect(Collectors.reducing("转换成字符串", in -> {return in + "";}, (perv, cur) -> {return perv + "," + cur;}));System.out.print(joinStr); // 转换成字符串,1,2,3,4,5,6,7,8,9}
Collectors.mapping+Collectors.reducing+TreeSet等等
@Testpublic void demo9(){// 求最大值 3List<Integer> list = Arrays.asList(1, 2, 3);Integer maxValue = list.stream().collect(Collectors.collectingAndThen(Collectors.maxBy((a, b) -> a - b), Optional::get));System.out.println("最大值:"+maxValue);// 最小值 1Integer minValue = list.stream().collect(Collectors.collectingAndThen(Collectors.minBy((a, b) -> a - b), Optional::get));System.out.println("最小值:"+minValue);// 求和 6Integer sumValue = list.stream().collect(Collectors.summingInt(item -> item));System.out.println("求和:"+sumValue);// 平均值 2.0Double avg = list.stream().collect(Collectors.averagingDouble(x -> x));System.out.println("平均值:"+avg);// 映射:先对集合中的元素进行映射,然后再对映射的结果使用Collectors操作// A,B,CSystem.out.println("大写:"+Stream.of("a", "b", "c").collect(Collectors.mapping(x -> x.toUpperCase(), Collectors.joining(","))));// sum: 是每次累计计算的结果,b是Function的结果System.out.println("return:"+Stream.of(1, 3, 4).collect(Collectors.reducing(0, x -> x + 1, (sum, b) -> {System.out.println("sum:"+sum + "->b:" + b);return sum + b;})));// 注意reducing可以用于更复杂的累计计算,加减乘除或者更复杂的操作// result = 2 * 4 * 5 = 40System.out.println("return:"+Stream.of(1, 3, 4).collect(Collectors.reducing(1, x -> x + 1, (result, b) -> {System.out.println("result:"+result + "->b:" + b);return result * b;})));// Collectors.joining(",")的结果是:a,b,c 然后再将结果 x + "d"操作, 最终返回a,b,cdString str= Stream.of("a", "b", "c").collect(Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d"));System.out.println("str: "+str);List<Integer> list1 = Arrays.asList(1, 2, 3);// [10, 20, 30]List<Integer> collect = list1.stream().map(i -> i * 10).collect(Collectors.toList());// [20, 10, 30]Set<Integer> collect1 = list1.stream().map(i -> i * 10).collect(Collectors.toSet());// {key1=value:10, key2=value:20, key3=value:30}Map<String, String> collect2 = list1.stream().map(i -> i * 10).collect(Collectors.toMap(key -> "key" + key/10, value -> "value:" + value));// [1, 3, 4]TreeSet<Integer> collect3= Stream.of(1, 3, 4).collect(Collectors.toCollection(TreeSet::new));System.out.println(collect+"\n"+collect1+"\n"+collect2+"\n"+collect3)}