当前位置:网站首页>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 ;
}
边栏推荐
- Data communication foundation smart_ Link_&_ Monitor_ Link
- 五种常见的咨询公司谈判策略以及如何维护自己的利益
- Appium自动化测试基础 — APPium基础操作API(一)
- sql中查询最近一条记录
- SQL injection sqllabs (basic challenges) 11-20
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
- 复现Thinkphp 2.x 任意代码执行漏洞
- Detailed explanation of C language branch statements
- Noi / 1.5 06: element maximum span value of integer sequence
- obj解析为集合
猜你喜欢
Appium自动化测试基础 — APPium基础操作API(一)
基于OpenHarmony的智能金属探测器
lvgl 显示图片示例
视觉体验全面升级,豪威集团与英特尔Evo 3.0共同加速PC产业变革
vlunhub- BoredHackerBlog Moriarty Corp
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
list去重并统计个数
JS knowledge points-01
超分辨率技术在实时音视频领域的研究与实践
Write a go program with vscode in one article
随机推荐
MySQL表字段调整
Severlet learning foundation
Maximum common subsequence
【簡記】解决IDE golang 代碼飄紅報錯
MySQL 巨坑:update 更新慎用影响行数做判断!!!
wyt 。。
D-snow halo solution
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
Vulnhub-Moneybox
【 note 】 résoudre l'erreur de code IDE golang
obj集合转为实体集合
Detailed explanation of C language branch statements
Codasip为RISC-V处理器系列增加Veridify安全启动功能
OSI seven layer model
go语言编程规范梳理总结
一文带你吃透js处理树状结构数据的增删改查
五种常见的咨询公司谈判策略以及如何维护自己的利益
20. [stm32] realize the function of intelligent garbage can by using ultrasonic module and steering gear
Misc Basic test method and knowledge points of CTF
开发中Boolean类型使用遇到的坑