当前位置:网站首页>对金额进行求和
对金额进行求和
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}
边栏推荐
- torch. nn. functional. Interpolate function
- JS的Proxy
- Technology T3 domestic platform! Successfully equipped with "Yihui domestic real-time system sylixos"
- DTD modeling
- 新增订单如何防止重复提交
- Object creation
- Optimization of video streaming with repeated requests in the case of unstable easygbs network
- How to configure webrtc video streaming format for easygbs, a new version of national standard gb28181 video platform?
- Transaction isolation level gap lock deadlock
- ffmpeg 错误码
猜你喜欢
【AI服务器搭建】CUDA环境
Use the uni app demo provided by Huanxin to quickly realize one-on-one chat
ddr4测试-2
解决VSCode下载慢或下载失败的问题
GC垃圾回收
JS 之 常用内置类的使用
[research materials] national second-hand housing market monthly report January 2022 - Download attached
Flutter 实战-快速实现音视频通话应用
Introduction and installation of crunch, and making password dictionary with crunch
Actual combat of flutter - fast implementation of audio and video call application
随机推荐
optaplanner学习笔记(一)案例Cloud balance
mysql 报错 Can‘t create table ‘demo01.tb_Student‘ (errno: 150)*
Class loading mechanism
JVM memory model
CMU AI PhD first year summary
[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached
Crunch简介、安装,使用Crunch制作密码字典
GC垃圾回收
703. 数据流中的第 K 大元素
Axure does not display catalogs
【无标题】
直播HLS协议
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Battery simulation of gazebo robot
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
703. The k-th element in the data flow
Live HLS protocol
GB28181之SIP协议
MySQL reports an error can't create table 'demo01 tb_ Student‘ (errno: 150)*
Transaction isolation level gap lock deadlock