原始数据
想要达到的效果数据
通过字段1进行分组然后合并字段2的数据值
通过 jdk8 的新特性list中stream流进行分组并合并字段
List<VResPortUse> vResPortLists = new ArrayList<>();
if (!CollectionUtils.isEmpty(nes)) {
vResPortLists = neManager.getPortReportInfo(portReportDO);
}
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;
}
private Long neId;
private Integer portTypeId;
private String portTypeName;
private Long total;
private Long used;
private String neName;
private Integer neTypeId;
private String neTypeName;
private Integer neTypeCategoryId;
private String neTypeCategoryName;
private Integer corpId;
private String corpName;
private String subnetPath;
private Long parentSubnetId;
private String parentSubnetName;
}