当前位置:网站首页>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 ;
}
边栏推荐
- Advanced level of static and extern
- 示例项目:简单的六足步行者
- D-snow halo solution
- Data communication foundation - routing communication between VLANs
- go语言编程规范梳理总结
- Memo 00
- How to introduce devsecops into enterprises?
- Quick completion guide for manipulator (IX): forward kinematics analysis
- 视觉体验全面升级,豪威集团与英特尔Evo 3.0共同加速PC产业变革
- Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
猜你喜欢
16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature
The OBD deployment mode of oceanbase Community Edition is installed locally
vlunhub- BoredHackerBlog Social Network
Detailed explanation of C language branch statements
研发效能度量指标构成及效能度量方法论
Clock switching with multiple relationship
Bubble sort, insert sort
Appium automation test foundation - appium basic operation API (I)
Lesson 4 knowledge summary
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
随机推荐
Summary of the second lesson
Why should we learn mathematical modeling?
SQL injection sqllabs (basic challenges) 11-20
五种常见的咨询公司谈判策略以及如何维护自己的利益
Six common transaction solutions, you sing, I come on stage (no best, only better)
17. [stm32] use only three wires to drive LCD1602 LCD
F. Min cost string problem solving Report
复现Thinkphp 2.x 任意代码执行漏洞
Array sorting num ranking merge in ascending order
Quick completion guide for manipulator (IX): forward kinematics analysis
【簡記】解决IDE golang 代碼飄紅報錯
机械臂速成小指南(九):正运动学分析
Intelligent metal detector based on openharmony
lv_ font_ Conv offline conversion
How can the boss choose programmers to help me with development?
vlunhub- BoredHackerBlog Social Network
swiper. JS to achieve barrage effect
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
Codasip为RISC-V处理器系列增加Veridify安全启动功能
Boost the development of digital economy and consolidate the base of digital talents - the digital talent competition was successfully held in Kunming