当前位置:网站首页>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 ;
}
边栏推荐
- 具有倍数关系的时钟切换
- 16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度
- vulnhub-Root_ this_ box
- OSI seven layer model
- Vulnhub-Moneybox
- 定义严苛标准,英特尔Evo 3.0正在加速PC产业升级
- Quick completion guide for manipulator (IX): forward kinematics analysis
- Bugku easy_ nbt
- Analytic hierarchy process of mathematical modeling (including Matlab code)
- Bugku's eyes are not real
猜你喜欢
swiper. JS to achieve barrage effect
Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022
Vulnhub-Moneybox
Codasip adds verify safe startup function to risc-v processor series
Number protection AXB function! (essence)
Example project: simple hexapod Walker
I spring and autumn blasting-2
vlunhub- BoredHackerBlog Moriarty Corp
Six common transaction solutions, you sing, I come on stage (no best, only better)
lv_ font_ Conv offline conversion
随机推荐
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
How can the boss choose programmers to help me with development?
JS knowledge points-01
CODING DevSecOps 助力金融企业跑出数字加速度
Install PHP extension spoole
Analytic hierarchy process of mathematical modeling (including Matlab code)
Data communication foundation - dynamic routing protocol rip
MySQL overview
Anti shake and throttling
The OBD deployment mode of oceanbase Community Edition is installed locally
Linear DP (basic questions have been updated)
swiper. JS to achieve barrage effect
Summary of the second lesson
Bugku's steganography
D-snow halo solution
Data communication foundation smart_ Link_&_ Monitor_ Link
Defining strict standards, Intel Evo 3.0 is accelerating the upgrading of the PC industry
OSI seven layer model
MySQL table field adjustment
The computer is busy, and the update is a little slow