当前位置:网站首页>reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
2022-07-02 17:26:00 【yuhui66666688gfbfdy】
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
@Test
public void testReduce() {
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8});
Stream<BigDecimal> stream1 = Arrays.stream(new BigDecimal[]{1, 2, 3, 4, 5, 6, 7, 8});
没有返回0
BigDecimal sum = stream1.stream().reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
//求集合元素只和 初始值为0
Integer result = stream.reduce(0, Integer::sum);
System.out.println(result);
stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
//求和
stream.reduce((i, j) -> i + j).ifPresent(System.out::println);
stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
//求最大值
stream.reduce(Integer::max).ifPresent(System.out::println);
stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
//求最小值
stream.reduce(Integer::min).ifPresent(System.out::println);
stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
//做逻辑
stream.reduce((i, j) -> i > j ? j : i).ifPresent(System.out::println);
stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
//求逻辑求乘机
int result2 = stream.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j);
Optional.of(result2).ifPresent(System.out::println);
}
边栏推荐
- R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
- Websocket of Web real-time communication technology
- sql训练2
- Typical application of "stack" - expression evaluation (implemented in C language)
- Troubleshooting ideas that can solve 80% of faults
- Leetcode interview question 16.11 Diving board
- [daily question] first day
- 怎么用ps提取图片颜色分析色彩搭配
- Redis(6)----对象与数据结构
- Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
猜你喜欢
Leetcode(81)——搜索旋转排序数组 II
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
Deep learning mathematics foundation
300+ documents! This article explains the latest progress of multimodal learning based on transformer
Hongmeng's fourth learning
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
医院在线问诊源码 医院视频问诊源码 医院小程序源码
Excel如何进行隔行复制粘贴
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
随机推荐
Progress-进度条
@Component cannot get Dao layer
如何优雅的写 Controller 层代码?
Nm02 nm module call sequence diagram and code interpretation independent of bus protocol
Night God simulator +fiddler packet capture test app
Redis (7) -- database and expiration key
Web实时通信技术之Websocket
材质UV遮罩的技巧
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
IPtable port redirection masquerade[easy to understand]
The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown
How to write controller layer code gracefully?
Leetcode interview question 16.15 Abacus wonderful calculation
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
Deep learning mathematics foundation
一款简约PHP个人发卡程序V4.0版本
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?