当前位置:网站首页>对金额进行求和
对金额进行求和
2022-07-01 18:47:00 【码农巅峰】
一:准备实体类
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
@Data
@Accessors(chain = true)
public class Car {
/**
* 车名称
*/
private String carName;
/**
* 车价格
*/
private BigDecimal price;
/**
* 轮胎数量
*/
private Integer tyreCount;
}
二:测试案例
package cn.nhdc.cloud.common.utils;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<Car> cars = Arrays.asList(new Car().setCarName("小红").setPrice(new BigDecimal("200000.55")).setTyreCount(5),
new Car().setCarName("小绿").setPrice(new BigDecimal("300000.22")).setTyreCount(4),
new Car().setCarName("小黄").setPrice(new BigDecimal("125000")).setTyreCount(6),
new Car().setCarName("小黄").setPrice(new BigDecimal("205000.11")).setTyreCount(4));
//计算车的价格 第一种 有精度损失
BigDecimal sum1 = BigDecimal.valueOf(cars.stream().mapToDouble((car) -> car.getPrice().doubleValue()).sum());
//计算车的价格 第二种 有精度损失
BigDecimal sum2 = BigDecimal.valueOf(cars.stream().collect(Collectors.summingDouble((car)-> car.getPrice().doubleValue())));
//计算车的价格 第三种 有精度损失
BigDecimal sum3 = BigDecimal.valueOf(cars.stream().mapToDouble((car) -> car.getPrice().doubleValue()).summaryStatistics().getSum());
//计算车的价格 第四种 推荐
BigDecimal sum4 = cars.stream().map(Car::getPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
//计算车的价格 第五种 推荐
BigDecimal sum5 = cars.stream().map(Car::getPrice).reduce(BigDecimal::add).get();
System.err.println(sum1);
System.err.println(sum2);
System.err.println(sum3);
System.err.println(sum4);
System.err.println(sum5);
//组完成之后并按照价格汇总 第一种
Map<String, BigDecimal> collect= cars.stream().collect(Collectors.groupingBy(x->x.getCarName(),
Collectors.reducing(BigDecimal.ZERO,
t->t.getPrice(), (v1, v2) -> v1.add(v2))));
//分组完成之后并按照价格汇总 第二种
Map<String, BigDecimal> collect2= cars.stream().collect(Collectors.groupingBy(x->x.getCarName(),
Collectors.reducing(BigDecimal.ZERO,
t->t.getPrice(), BigDecimal::add)));
System.err.println(collect);
System.err.println(collect2);
}
}
三:运行结果
830000.8799999999
830000.8799999999
830000.8799999999
830000.88
830000.88
{小红=200000.55, 小绿=300000.22, 小黄=330000.11}
{小红=200000.55, 小绿=300000.22, 小黄=330000.11}
边栏推荐
- 大厂音视频职位面试题目--今日头条
- 一文读懂C语言中的结构体
- pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']
- [exercise] HashSet
- Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
- 类加载机制
- Opencv video quality detection -- sharpness detection
- 较真儿学源码系列-InheritableThreadLocal(逐行源码带你分析作者思路)
- What is the essential difference between Bi development and report development?
- Ffmpeg error code
猜你喜欢
![[Mori city] random talk on GIS data (I)](/img/4d/1ed4bbf397b8d756c91241705528de.jpg)
[Mori city] random talk on GIS data (I)

Crunch简介、安装,使用Crunch制作密码字典

Uni app product classification

Introduction and installation of crunch, and making password dictionary with crunch

JS的Proxy

Wireshark packet analysis TCP, FTP

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?

MySQl的基本使用

Cookie和Session的相关概念
![Thesis reading [distinctive late semantic graph for video capturing]](/img/d4/4f84a73a9127fa87bb0a74c4655d15.png)
Thesis reading [distinctive late semantic graph for video capturing]
随机推荐
Interview question 16.16 Partial sorting - Double finger needling
JS ternary expression complex condition judgment
Flutter 实战-快速实现音视频通话应用
实例讲解将Graph Explorer搬上JupyterLab
mysql 报错 Can‘t create table ‘demo01.tb_Student‘ (errno: 150)*
[exercise] HashSet
Using win7 vulnerability to crack the system login password
P2433 [deep foundation 1-2] primary school mathematics n in one
ffmpeg AVFrame 转 cv::Mat
New window open page -window open
【无标题】
Ffmpeg audio related commands
DTD modeling
Wechat applet realizes keyword highlighting
Interview questions for audio and video positions in Dachang -- today's headline
ES6中的代理proxy
为定时器和延时器等其它情况的回调函数绑定当前作用域的this
DDR4 test-2
[research materials] Huawei Technology ICT 2021: at the beginning of the "Yuan" year, the industry is "new" -- download attached
Uni app product classification