当前位置:网站首页>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 ;
}
边栏推荐
- RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022
- Maximum common subsequence
- Summary of the third class
- Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
- [brief notes] solve the problem of IDE golang code red and error reporting
- Severlet learning foundation
- Six common transaction solutions, you sing, I come on stage (no best, only better)
- Install PHP extension spoole
- Detailed explanation of C language branch statements
- Ionic Cordova project modification plug-in
猜你喜欢

机械臂速成小指南(九):正运动学分析

Analytic hierarchy process of mathematical modeling (including Matlab code)

Write a go program with vscode in one article

Quick completion guide for manipulator (IX): forward kinematics analysis

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

keep-alive

F. Min cost string problem solving Report

OSI 七层模型

具有倍数关系的时钟切换

Data communication foundation - route republication
随机推荐
Memo 00
研发效能度量指标构成及效能度量方法论
The computer is busy, and the update is a little slow
Bugku's Ah Da
记录一下树莓派搭建环境中遇到的坑。。。
Bugku telnet
Transfer the idea of "Zhongtai" to the code
Noi / 1.5 06: element maximum span value of integer sequence
SQL injection sqllabs (basic challenges) 1-10
Data communication foundation - routing communication between VLANs
我们为什么要学习数学建模?
JS topic - console log()
vulnhub-Root_ this_ box
Six common transaction solutions, you sing, I come on stage (no best, only better)
ICML 2022 | explore the best architecture and training method of language model
Bugku's Ping
MySQL5.7的JSON基本操作
Example project: simple hexapod Walker
OSI 七层模型
Misc Basic test method and knowledge points of CTF