当前位置:网站首页>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 ;
}
边栏推荐
- Noi / 1.4 07: collect bottle caps to win awards
- Can gbase 8A view the location of SQL statement history?
- MySQL overview
- wyt 。。
- RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022
- Number protection AXB function! (essence)
- vulnhub-Root_ this_ box
- Record the pits encountered in the raspberry pie construction environment...
- Bugku's eyes are not real
- F. Min cost string problem solving Report
猜你喜欢

Bugku alert

示例项目:简单的六足步行者

Codasip adds verify safe startup function to risc-v processor series

Data communication foundation - routing communication between VLANs

【簡記】解决IDE golang 代碼飄紅報錯

Data communication foundation - route republication

Example project: simple hexapod Walker

CSRF, XSS science popularization and defense

Appium automation test foundation - appium basic operation API (I)

Common PHP interview questions (1) (written PHP interview questions)
随机推荐
Ionic Cordova project modification plug-in
Memo 00
queryRunner. Query method
ICML 2022 | explore the best architecture and training method of language model
Fundamentals of data communication - Principles of IP routing
复现Thinkphp 2.x 任意代码执行漏洞
事务回滚异常
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
wyt 。。
Noi / 1.5 37: mercenaries
示例项目:简单的六足步行者
SQL injection sqllabs (basic challenges) 11-20
一文搞定vscode编写go程序
【 note 】 résoudre l'erreur de code IDE golang
Optional parameters in the for loop
我们为什么要学习数学建模?
Record the pits encountered in the raspberry pie construction environment...
Write a go program with vscode in one article
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Xiao Sha's arithmetic problem solving Report