原文地址:https://mkyong.com/java8/java-8-stream-reduce-examples/
作者:mkyong
翻译:高行行
在 Java 8 中,Stream.reduce()
合并流的元素并产生单个值。
使用 for 循环的简单求和运算。
1 2 3 4 5 6 7 COPY int [] numbers = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };int sum = 0 ;for (int i : numbers) { sum += i; } System.out.println("sum : " + sum);
相当于 Stream.reduce()
1 2 3 4 5 6 COPY int [] numbers = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };int sum = Arrays.stream(numbers).reduce(0 , (a, b) -> a + b);System.out.println("sum : " + sum);
或方法引用 Integer::sum
1 COPY int sum = Arrays.stream(numbers).reduce(0 , Integer::sum);
Integer.java
1 2 3 4 5 6 7 8 9 10 11 12 COPY public static int sum (int a, int b) { return a + b; }
1. 方法签名 1.1 查看Stream.reduce()
方法签名: Stream.java
1 COPY T reduce (T identity, BinaryOperator<T> accumulator) ;
IntStream.java
1 COPY int reduce (int identity, IntBinaryOperator op) ;
LongStream.java
1 COPY long reduce (int identity, LongBinaryOperator op) ;
identity = 默认值或初始值。
BinaryOperator = 函数式接口,取两个值并产生一个新值。(注: java Function 函数中的 BinaryOperator 接口用于执行 lambda 表达式并返回一个 T 类型的返回值)
1.2 如果缺少identity
参数,则没有默认值或初始值,并且它返回 optional。 Stream.java
1 COPY Optional<T> reduce (BinaryOperator<T> accumulator) ;
2. 更多例子 2.1 数学运算。 1 2 3 4 5 6 7 8 COPY int [] numbers = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };int sum = Arrays.stream(numbers).reduce(0 , (a, b) -> a + b); int sum2 = Arrays.stream(numbers).reduce(0 , Integer::sum); int sum3 = Arrays.stream(numbers).reduce(0 , (a, b) -> a - b); int sum4 = Arrays.stream(numbers).reduce(0 , (a, b) -> a * b); int sum5 = Arrays.stream(numbers).reduce(0 , (a, b) -> a / b);
2.2 最大和最小 1 2 3 4 5 6 7 COPY int [] numbers = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };int max = Arrays.stream(numbers).reduce(0 , (a, b) -> a > b ? a : b); int max1 = Arrays.stream(numbers).reduce(0 , Integer::max); int min = Arrays.stream(numbers).reduce(0 , (a, b) -> a < b ? a : b); int min1 = Arrays.stream(numbers).reduce(0 , Integer::min);
2.3 连接字符串。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 COPY String[] strings = {"a" , "b" , "c" , "d" , "e" }; String reduce = Arrays.stream(strings).reduce("" , (a, b) -> a + "|" + b);String reduce2 = Arrays.stream(strings).reduce("" , (a, b) -> { if (!"" .equals(a)) { return a + "|" + b; } else { return b; } }); String join = String.join("|" , strings);
3. Map & Reduce 一个简单的 map 和 reduce 示例,用于从发票 List 中求 BigDecimal 的和。
JavaReduce.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 COPY package com.mkyong;import java.math.BigDecimal;import java.math.RoundingMode;import java.util.Arrays;import java.util.List;public class JavaReduce { public static void main (String[] args) { List<Invoice> invoices = Arrays.asList( new Invoice ("A01" , BigDecimal.valueOf(9.99 ), BigDecimal.valueOf(1 )), new Invoice ("A02" , BigDecimal.valueOf(19.99 ), BigDecimal.valueOf(1.5 )), new Invoice ("A03" , BigDecimal.valueOf(4.99 ), BigDecimal.valueOf(2 )) ); BigDecimal sum = invoices.stream() .map(x -> x.getQty().multiply(x.getPrice())) .reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println(sum); System.out.println(sum.setScale(2 , RoundingMode.HALF_UP)); } } class Invoice { String invoiceNo; BigDecimal price; BigDecimal qty; }
输出