原始数据
字段1 | 字段2 |
---|---|
A | 2 |
A | 3 |
A | 4 |
B | 4 |
想要达到的效果数据
字段1 | 字段2 |
---|---|
A | 9 |
B | 4 |
通过字段1进行分组然后合并字段2的数据值
通过 jdk8 的新特性list中stream流进行分组并合并字段
//获取查询条件下视图的数据,即原始数据集合
List<VResPortUse> vResPortLists = new ArrayList<>();
if (!CollectionUtils.isEmpty(nes)) {
vResPortLists = neManager.getPortReportInfo(portReportDO);
}
//放到新的list集合中去
List<VResPortUse> newVResPortLists = new ArrayList<>();
//按照网元进行分组后计算被用端口和端口总数
vResPortLists.parallelStream()
.collect(Collectors.groupingBy(item -> (item .getNeId()), Collectors.toList()))
.forEach((id, transfer) -> {
transfer.stream()
.reduce((a, b) ->
new VResPortUse(a.getNeId(),
a.getPortTypeId(),
a.getPortTypeName(),
a.getTotal() + b.getTotal(),
a.getUsed() + b.getUsed(),
a.getNeName(),
a.getPortTypeId(),
a.getNeTypeName(),
a.getNeTypeCategoryId(),
a.getNeTypeCategoryName(),
a.getCorpId(),a.getCorpName(),
a.getSubnetPath(),a.getParentSubnetId(),
a.getParentSubnetName()
))
.ifPresent(newVResPortLists::add);
});
实体类
@Data
public class VResPortUse {
public VResPortUse() {
}
public VResPortUse(Long neId, Integer portTypeId, String portTypeName, Long total, Long used, String neName, Integer neTypeId, String neTypeName, Integer neTypeCategoryId, String neTypeCategoryName, Integer corpId, String corpName, String subnetPath, Long parentSubnetId, String parentSubnetName) {
this.neId = neId;
this.portTypeId = portTypeId;
this.portTypeName = portTypeName;
this.total = total;
this.used = used;
this.neName = neName;
this.neTypeId = neTypeId;
this.neTypeName = neTypeName;
this.neTypeCategoryId = neTypeCategoryId;
this.neTypeCategoryName = neTypeCategoryName;
this.corpId = corpId;
this.corpName = corpName;
this.subnetPath = subnetPath;
this.parentSubnetId = parentSubnetId;
this.parentSubnetName = parentSubnetName;
}
/**
* 所属网元id(查询辅助列)
*/
private Long neId;
/**
* 端口类型
*/
private Integer portTypeId;
/**
* 端口类型名称
*/
private String portTypeName;
private Long total;
private Long used;
/**
* 网元名称
*/
private String neName;
/**
* 网元型号
*/
private Integer neTypeId;
/**
* 网元型号名称
*/
private String neTypeName;
/**
* 网元型号分类id
*/
private Integer neTypeCategoryId;
/**
* 描述信息
*/
private String neTypeCategoryName;
/**
* 厂商id,自增长主键
*/
private Integer corpId;
/**
* 厂商名称
*/
private String corpName;
/**
* 从顶层子网开始的路径(子网与子网之间以.隔开),禁止其他表使用
*/
private String subnetPath;
/**
* 所在子网id,0代表顶层子网
*/
private Long parentSubnetId;
private String parentSubnetName;
}
版权声明:本文为weixin_43447266原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。