API documentation
The stream stream overloads the reduce method:
The first:
T reduce(T identity,BinaryOperator<T> accumulator)
Let's try to modify the code to see the meaning of each parameter:
add
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,2,3,4,5);
int result = numList.stream().reduce(0,(a,b) -> a + b );
System.out.println(result);
}
}
// 结果为:15
sub:
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,2,3,4,5);//Arrays的asList()方法 可以将 数组转为List
int result = numList.stream().reduce(0,(a,b) -> a - b );
System.out.println(result);
}
}
// 结果为:-15
I have:
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,2,3,4,5);//Arrays的asList()方法 可以将 数组转为List
int result = numList.stream().reduce(0,(a,b) -> a * b );
System.out.println(result);
}
}
// 结果为:0
The result of running here is 0. Why is this? ?
Try to modify the first parameter identity of reduce to 1 here:
int result = numList.stream().reduce(1,(a,b) -> a * b );
//运行结果:120
When identity=2:
int result = numList.stream().reduce(2,(a,b) -> a * b );
//运行结果:240
Check the related blog , here the first parameter of the expression is used to specify an initial value.
Equivalent to 240 = 2 1 2 3 4*5;
Div:
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,2,3,4,5);//Arrays的asList()方法 可以将 数组转为List
int result = numList.stream().reduce(120,(a,b) -> a / b );
System.out.println(result);
}
}
// 结果为:1
This code can be clearly seen.
Add a try for strings
public class Test1 {
public static void main(String[] args) {
String str1 = "a";
int ans = str1.chars().reduce('a', (a, b) -> a - b);
System.out.println(ans);
}
}
// 结果为:0
Here is the subtraction of the ASCII code value.
XOR:
public class Test1 {
public static void main(String[] args) {
String str1 = "a";
int ans = str1.chars().reduce('a', (a, b) -> a ^ b);
System.out.println(ans);
}
}
// 结果为:0
The second type:
Optional<T> reduce(BinaryOperator<T> accumulator)
Then continue to try,
add
List<Integer> numList = Arrays.asList(1,2,3,4,5);
int result = numList.stream().reduce((a,b) -> a + b ).get();
System.out.println(result);
//运行结果:15
I have
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,2,3,4,5);
int result = numList.stream().reduce((a,b) -> a * b ).get();
System.out.println(result);
}
}
// 结果为:120
The difference from the first is the return value here, which needs to be obtained by get.
Let's look at another code and its execution result:
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,2,3,4,5);
int result = numList.stream().reduce((a,b) -> {
System.out.println("a=" + a + ",b=" + b);
return a + b;
} ).get();
}
}
The a parameter of the lambada expression is the cache of the execution result of the expression, that is, the execution result of the expression this time will be used as the parameter of the next execution, and the second parameter b is each element in the stream in turn. If the expression is executed for the first time, a is the first element in the stream.
The third type:
<U> U reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner)
Continue to try:
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(Integer.MAX_VALUE,Integer.MAX_VALUE);
System.out.println(numList);
long result = numList.stream().reduce(0L,(a,b) -> a + b, (a,b)-> 0L );
System.out.println(result);
}
}
运行结果:
[2147483647, 2147483647]
4294967294
It can be seen that this makes up for the shortcomings of the first two, their calculation results must be the same as the element type in the stream, the third is not required.
Other applications: Convert an ArrayList of int type to an ArrayList of String type
public class Test1 {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1, 2, 3, 4, 5, 6);
ArrayList<String> result = numList.stream().reduce(new ArrayList<String>(), (a, b) -> {
a.add("element-" + Integer.toString(b));
return a;
}, (a, b) -> null);
System.out.println(result);
}
}
运行结果:
[element-1, element-2, element-3, element-4, element-5, element-6]
Don’t try too much for the third one here. If you are interested, you can try