当前位置:网站首页>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);
}
边栏推荐
- The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
- R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
- options should NOT have additional properties
- 300+篇文献!一文详解基于Transformer的多模态学习最新进展
- 任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
- 深度学习数学基础
- Meta universe chain game system development (logic development) - chain game system development (detailed analysis)
- Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
- 【每日一题】第二天
- Leetcode interview question 17.04 Vanishing numbers
猜你喜欢

新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島

Mini Golf Course: a good place for leisure and tourism in London

Excel如何进行隔行复制粘贴
![[fluent] dart data type (VaR data type | object data type)](/img/1b/fe2529af5f6663fad1fb7861f14ab5.jpg)
[fluent] dart data type (VaR data type | object data type)

Redis(7)----数据库与过期键

LightGroupButton* sender = static_ cast<LightGroupButton*>(QObject::sender());

How to clean up discarded PVs and their corresponding folders

UE4 draw a circle with spline
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

SLC、MLC、TLC 和 QLC NAND SSD 之间的区别:哪个更好?
随机推荐
UE4 用spline畫正圓
Redis(6)----对象与数据结构
Basic idea of quick sorting (easy to understand + examples) "suggestions collection"
[daily question] first day
Leetcode interview question 17.04 Vanishing numbers
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
Which securities company has a low, safe and reliable online account opening commission
STM32G0 USB DFU 升级校验出错-2
[daily question] the next day
The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown
StretchDIBits函数
Rte11 interrupt decoupling function
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
元宇宙链游系统开发(逻辑开发)丨链游系统开发(详细分析)
Uncover the whole link communication process of dewu customer service im
如何优雅的写 Controller 层代码?
Typical application of "stack" - expression evaluation (implemented in C language)
What are the links of the problem
Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0
在支付宝账户上买基金安全吗