当前位置:网站首页>List uses stream flow to add according to the number of certain attributes of the element
List uses stream flow to add according to the number of certain attributes of the element
2022-07-05 15:58:00 【Ant HJK】
Need to be on a List Remove duplicate unique value attributes of objects in , Attribute summation , The object is assumed to be BillsNums, Yes id、nums、sums Three attributes , among id Represents a unique value , need nums And sums In sum , And finally keep one .
For example, :(“s1”, 1, 1),(“s1”,2,3),(“s2”,4,4), Sum and remove the weight , Namely (“s1”, 3, 4),(“s2”,4,4)
Objects and properties
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;
}
}
data
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));
}
/**
* take id A merger nums, sums The set after adding a round and using Java8 Process the stream
*/
public static List<BillsNums> merge(List<BillsNums> list) {
List<BillsNums> result = list.stream()
// Express id by key, Then if there is a repetition , So from BillsNums object o1 And o2 Filter out one , Choose here o1,
// And put id repeat , Need to put nums and sums And o1 To merge o2, Assign a value to o1, Finally back to 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 ;
}
边栏推荐
- Severlet learning foundation
- F. Weights assignment for tree edges problem solving Report
- Memo 00
- Noi / 1.4 07: collect bottle caps to win awards
- vulnhub-Root_ this_ box
- 【簡記】解决IDE golang 代碼飄紅報錯
- list去重并统计个数
- SQL injection sqllabs (basic challenges) 1-10
- Array sorting num ranking merge in ascending order
- Intelligent metal detector based on openharmony
猜你喜欢

定义严苛标准,英特尔Evo 3.0正在加速PC产业升级

MySQL 巨坑:update 更新慎用影响行数做判断!!!

SQL injection sqllabs (basic challenges) 11-20

Why should we learn mathematical modeling?

Data communication foundation ACL access control list

ES6深入—ES6 Generator 函数

lv_ font_ Conv offline conversion

Analytic hierarchy process of mathematical modeling (including Matlab code)

Write a go program with vscode in one article

I spring and autumn blasting-1
随机推荐
一文搞定vscode编写go程序
I spring and autumn blasting-2
I spring web upload
研发效能度量指标构成及效能度量方法论
Codasip为RISC-V处理器系列增加Veridify安全启动功能
episodic和batch的定义
Codasip adds verify safe startup function to risc-v processor series
JS knowledge points-01
Data communication foundation - Ethernet port mirroring and link aggregation
First PR notes
Value series solution report
SQL injection sqllabs (basic challenges) 11-20
list集合根据对象某属性求和,最大值等
JS topic - console log()
写单元测试的时候犯的错
Memo 00
CSDN I'm coming
MySQL5.7的JSON基本操作
Xiao Sha's arithmetic problem solving Report
Transfer the idea of "Zhongtai" to the code