如何使用lamda表达式对list进行求和
作者:爱钓鱼的Java 时间:2022-08-24 09:20:09
使用lamda表达式对list进行求和
Lambda 表达式是 JDK8 的一个新特性,最近写项目中求和计算使用的较多,写篇文章记录下。
1、实体类List返回Integer类型求和
//根据id查询库存
List<ProductStock> list = productStockMapper.selectList(Wrappers.<ProductStock>lambdaQuery().in(Product::id, idList))
//查询所有库存总和
Integer qtySum = list.stream().mapToInt(ProductStock::getStockQty).sum();
2、Integer类型List返回Integer类型求和
//查询当前所有商品库存
List<Integer> stockList = productStockMapper.selectStock();
//查询所有库存总和
Integer stockSum = stockList .stream().mapToInt(Integer::intValue).sum();
3、实体类List返回Bigdecimal类型求和
//根据id查询库存
List<ProductStock> list = productStockMapper.selectList(Wrappers.<ProductStock>lambdaQuery().in(Product::id, idList))
//查询所有库存金额总和
BigDecimal stockAmt = list .stream().map(ProductStock::getStockAmt).reduce(BigDecimal::add).orElse(Bigdecimal.ZERO);
4、BigDecimal类List返回Bigdecimal类型求和
//查询当前所有商品库存
List<Bigdecimal> stockList = productStockMapper.selectStock();
//查询所有库存金额总和
Bigdecimal stockAmt = stockList.stream().reduce(BigDecimal::add).orElse(Bigdecimal.ZERO);
**注:orElse(Bigdecimal.ZERO)而不是get(),是因为防止返回空(此处如果用get(),在idea中也会有警告)。
最近用到的就这些,后续用到别的还会再更新!
list与Lamda表达式配合的常用方法
1、删除所有值为400的元素
list.RemoveAll(e=>e==400);
2、删除所有能被100整除的元素
list.RemoveAll(e=>e%100==0);
3、求和
int sum=0;
int result = list.ForEach(val=>sum+=val);
4、删除所有值为400的元素
bool result = list.Exists(e=>e==400)
5、是否所有的元素都等于400
bool result = list.TrueForAll(e=>e==400)
6、返回能被100整除的元素(从前向后找)
var result = list.Find(e=>e%100==0);
6、返回能被100整除的元素(从后向前找)
var result = list.FindLast(e=>e%100==0);
7、返回能被100整除的List
var result = list.FindAll(e=>e%100==0);
8、返回能被100整除的索引(从前向后找)
int result = list.FindIndex(e=>e%100==0);
9、返回能被100整除的索引(从后向前找)
int result = list.FindLastIndex(e=>e%100==0);
10、二分查找(速度较快,它的原理是先把排序好的list分成2分,搜索中点值,发现值不对,就可以砍掉这个分组,只剩下一半再查找)
list.Sort();//二分查找前先必须先升序排序
int result = list.BinarySearch(e=>e%100==0);
11、对类(引用类型)进行排序(bookList.Sort()),需要类实现IComparable接口
internal class Book : IComparable<Book>
{
public int ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public int CompareTo([AllowNull] Book other)
{
if (other == null) return 1;
return this.ID - other.ID;//返回正数,this>other;返回0,this=other;返回负数,this<other;
}
public override string ToString()
{
return JsonSerializer.Serialize(this);
}
public override bool Equals(object obj)
{
if (obj == null) return false;
Book other = obj as Book;
if (other.ID == this.ID && other.Name == this.Name && other.Price == this.Price) return true;
return false;
}
}
12、对类(引用类型)进行二分查找(bookList.BinarySearch()),只能查找相应对象。如果只查找属性一致的对象,需重写Equals()方法,如上所示。
来源:https://blog.csdn.net/yuanwxcsdn/article/details/105663700
标签:lamda,表达式,list,求和
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
最简单易懂的java数组排序方法整理
2023-01-03 18:56:41
判断图片-判断位图是否是黑白图片的方法
2023-06-09 17:20:07
浅谈java+内存分配及变量存储位置的区别
2022-07-09 00:46:47
实例解析JAVA中代码的加载顺序
2021-10-26 14:57:22
![](https://img.aspxhome.com/file/2023/8/61038_0s.png)
SpringCloud Feign参数问题及解决方法
2022-08-08 08:00:35
mybatis-plus中lambdaQuery()与lambdaUpdate()比较常见的使用方法总结
2023-11-24 22:43:04
![](https://img.aspxhome.com/file/2023/6/60156_0s.jpg)
java微信企业号开发之通讯录
2022-04-28 12:51:40
![](https://img.aspxhome.com/file/2023/1/71911_0s.png)
Maven的porfile与SpringBoot的profile结合使用案例详解
2023-11-14 00:07:59
![](https://img.aspxhome.com/file/2023/2/58752_0s.png)
基于Java利用static实现单例模式
2021-12-20 06:07:32
微信跳一跳辅助Java代码实现
2022-03-31 19:12:03
c# socket编程udp客户端实现代码分享
2023-06-16 05:03:31
java虚拟机学习笔记进阶篇
2022-01-07 04:53:06
![](https://img.aspxhome.com/file/2023/9/69109_0s.png)
C#编程实现自定义热键的方法
2023-12-05 23:57:18
java弹幕小游戏1.0版本
2021-12-06 04:42:48
Java语法基础之运算符学习笔记分享
2021-12-18 02:13:17
![](https://img.aspxhome.com/file/2023/6/71866_0s.png)
RxJava+Retrofit+Mvp实现购物车
2022-03-22 16:24:48
![](https://img.aspxhome.com/file/2023/7/64957_0s.gif)
如何解决Spring in action @valid验证不生效的问题
2023-08-29 07:59:56
Springboot 在普通类型注入Service或mapper
2023-11-29 15:26:21
Java下变量大小写驼峰、大小写下划线、大小写连线转换
2022-04-19 15:20:18
![](https://img.aspxhome.com/file/2023/7/61067_0s.jpg)
Java String对象使用方法详解
2023-12-14 14:43:52