当前位置:网站首页>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 ;
}
边栏推荐
- 抽象类和接口的区别
- 示例项目:简单的六足步行者
- sql中set标签的使用
- How can the boss choose programmers to help me with development?
- Example project: simple hexapod Walker
- 19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
- Bugku cyberpunk
- Severlet learning foundation
- Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
- Explanation report of the explosion
猜你喜欢
![16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature](/img/9f/c91904b6b1d3a1e85c0b50e43972e5.jpg)
16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature

Number protection AXB function! (essence)

vlunhub- BoredHackerBlog Moriarty Corp

list去重并统计个数

Verilog realizes the calculation of the maximum common divisor and the minimum common multiple

Anti shake and throttling

Bugku's Eval

【网易云信】超分辨率技术在实时音视频领域的研究与实践

lv_ font_ Conv offline conversion

How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you
随机推荐
Example of lvgl display picture
OSI 七层模型
Boost the development of digital economy and consolidate the base of digital talents - the digital talent competition was successfully held in Kunming
Vulnhub-Moneybox
queryRunner. Query method
vulnhub-FirstBlood
17. [stm32] use only three wires to drive LCD1602 LCD
Object. defineProperty() - VS - new Proxy()
Linear DP (basic questions have been updated)
MySQL table field adjustment
[brief notes] solve the problem of IDE golang code red and error reporting
Codasip adds verify safe startup function to risc-v processor series
CSDN I'm coming
Bugku cyberpunk
力扣今日题-729. 我的日程安排表 I
lv_font_conv离线转换
SQL injection sqllabs (basic challenges) 1-10
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
写单元测试的时候犯的错
Dataarts studio data architecture - Introduction to data standards