当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

300+ documents! This article explains the latest progress of multimodal learning based on transformer

Leetcode interview question 16.11 Diving board

夜神模擬器+Fiddler抓包測試App

The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown

在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天

Hongmeng's fourth learning

Leetcode interview question 17.04 Vanishing numbers

Leetcode interview question 16.15 Abacus wonderful calculation

Installation of thingsboard, an open source IOT platform

距离度量 —— 杰卡德距离(Jaccard Distance)
随机推荐
Redis(6)----对象与数据结构
【JVM调优实战100例】01——JVM的介绍与程序计数器
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell
科技公司不同人对Bug的反应 | 每日趣闻
什么是云原生?这回终于能搞明白了!
UML class diagram
Hongmeng's fourth learning
Comprendre complètement le tutoriel de traitement de Point Cloud basé sur open3d!
Excel如何进行隔行复制粘贴
哪个券商公司网上开户佣金低又安全又可靠
电商系统中常见的 9 大坑,你踩过没?
消除IBM P750小机上的黄色报警灯[通俗易懂]
徹底搞懂基於Open3D的點雲處理教程!
第一次去曼谷旅游怎么玩?这份省钱攻略请收好
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
全链路数字化转型下,零售企业如何打开第二增长曲线
iptable端口重定向 MASQUERADE[通俗易懂]
LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());
深度学习数学基础