当前位置:网站首页>list使用Stream流进行根据元素某属性数量相加
list使用Stream流进行根据元素某属性数量相加
2022-07-05 15:26:00 【小蚂蚁hjk】
需要对一个List中的对象进行唯一值属性去重,属性求和,对象假设为BillsNums,有id、nums、sums三个属性,其中id表示唯一值,需要nums与sums进行求和,并最后保持一份。
例如说:(“s1”, 1, 1),(“s1”,2,3),(“s2”,4,4), 求和并去重的话,就是(“s1”, 3, 4),(“s2”,4,4)
对象与属性
class BillsNums {
private String id;
private int nums;
private int sums;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
public int getSums() {
return sums;
}
public void setSums(int sums) {
this.sums = sums;
}
}
数据
public static void main(String[] args) {
List<BillsNums> billsNumsList = new ArrayList<>();
BillsNums billsNums = new BillsNums();
billsNums.setId("1001");
billsNums.setNums(2);
billsNums.setSums(100);
billsNumsList.add(billsNums);
BillsNums billsNums2 = new BillsNums();
billsNums2.setId("1001");
billsNums2.setNums(3);
billsNums2.setSums(100);
billsNumsList.add(billsNums2);
List<BillsNums> result = merge(billsNumsList);
System.out.println("result:" + JSON.toJSONString(result, true));
}
/**
* 将id进行合并nums, sums 相加道回合并后的集合使用Java8的流进行处理
*/
public static List<BillsNums> merge(List<BillsNums> list) {
List<BillsNums> result = list.stream()
// 表示id为key, 接着如果有重复的,那么从BillsNums对象o1与o2中筛选出一个,这里选择o1,
// 并把id重复,需要将nums和sums与o1进行合并的o2, 赋值给o1,最后返回o1
.collect(Collectors.toMap(BillsNums::getId, a -> a, (o1,o2)-> {
o1.setNums(o1.getNums() + o2.getNums());
o1.setSums(o1.getSums() + o2.getSums());
return o1;
})).values().stream().collect(Collectors.toList());
return result ;
}
边栏推荐
- 项目sql中批量update的时候参数类型设置错误
- Optional parameters in the for loop
- ICML 2022 | explore the best architecture and training method of language model
- swiper. JS to achieve barrage effect
- Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
- 力扣今日题-729. 我的日程安排表 I
- MySQL table field adjustment
- Data communication foundation ACL access control list
- Bugku's Eval
- Misc Basic test method and knowledge points of CTF
猜你喜欢

Bugku cyberpunk

verilog实现计算最大公约数和最小公倍数

Intelligent metal detector based on openharmony

把 ”中台“ 的思想迁移到代码中去

How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you

SQL injection sqllabs (basic challenges) 11-20

Detailed explanation of C language branch statements

Quick completion guide for manipulator (IX): forward kinematics analysis
![19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)](/img/fe/8f59db28823290da8e9280df06673d.jpg)
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)

具有倍数关系的时钟切换
随机推荐
Analytic hierarchy process of mathematical modeling (including Matlab code)
基于OpenHarmony的智能金属探测器
Bugku cyberpunk
MySQL5.7的JSON基本操作
Data communication foundation smart_ Link_&_ Monitor_ Link
Six common transaction solutions, you sing, I come on stage (no best, only better)
Advanced level of static and extern
机械臂速成小指南(九):正运动学分析
Noi / 1.3 01: a+b problem
一文带你吃透js处理树状结构数据的增删改查
SQL injection sqllabs (basic challenges) 1-10
Write a go program with vscode in one article
I include of spring and Autumn
把 ”中台“ 的思想迁移到代码中去
事务回滚异常
Fundamentals of data communication - Principles of IP routing
Common MySQL interview questions (1) (written MySQL interview questions)
String modification problem solving Report
复现Thinkphp 2.x 任意代码执行漏洞
如何将 DevSecOps 引入企业?