当前位置:网站首页>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);
}
边栏推荐
- Crypto usage in nodejs
- 什么是云原生?这回终于能搞明白了!
- Websocket of Web real-time communication technology
- Nm02 nm module call sequence diagram and code interpretation independent of bus protocol
- Rte11 interrupt decoupling function
- Nm01 function overview and API definition of nm module independent of bus protocol
- Basic idea of quick sorting (easy to understand + examples) "suggestions collection"
- 如何设置VSCode删除整行快捷键?
- 如何优雅的写 Controller 层代码?
- CDN acceleration and breaking J anti-theft chain function
猜你喜欢
How to copy and paste interlaced in Excel
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
呆错图床系统源码图片CDN加速与破J防盗链功能
Exness in-depth good article: dynamic series - Case Analysis of gold liquidity (V)
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
Thoroughly understand the point cloud processing tutorial based on open3d!
Nm01 function overview and API definition of nm module independent of bus protocol
300+篇文献!一文详解基于Transformer的多模态学习最新进展
【每日一题】第二天
深度学习数学基础
随机推荐
阿里三面被面试官狂问Redis,简历上再也不敢写'精通'了
[daily question] first day
Which securities company has a low, safe and reliable online account opening commission
How to use PS to extract image color and analyze color matching
如何优雅的写 Controller 层代码?
Leetcode interview question 16.15 Abacus wonderful calculation
27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
300+ documents! This article explains the latest progress of multimodal learning based on transformer
Troubleshooting ideas that can solve 80% of faults
拦截器与过滤器的区别
Simulateur nightGod + application de test de capture de paquets Fiddler
[Yugong series] July 2022 go teaching course 001 introduction to go language premise
C语言中函数参数传递的三种方式
Leetcode(154)——寻找旋转排序数组中的最小值 II
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
呆错图床系统源码图片CDN加速与破J防盗链功能
How to copy and paste interlaced in Excel
Night God simulator +fiddler packet capture test app