package com.shrimpking.t3;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/9/11 18:06
*/
public class ReduceStream
{
public static void main(String[] args)
{
List<Integer> list = Arrays.asList(1,3,5,7,4,6);
Stream<Integer> stream = list.stream();
//Integer::max 等价于 (a,b) -> a > b ? a : b
Optional<Integer> max = stream.reduce(Integer::max);
System.out.println("最大值为:" + max.get());
stream = list.stream();
// (a,b)-> a + b 等价于 Integer::sum
Integer sum = stream.reduce(10, (a,b) -> a + b);
System.out.println("累加的和为:" + sum);
}
}
09-02
4万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-22
3688
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-27