Map<String, List<PortReportResultVo>> relayLinkAndPortMap = new HashMap<>(); for (Map.Entry<String, List<PortReportResultVo>> entry : relayLinkAndPortMap.entrySet()) { // key为链路组 String key = entry.getKey(); // value是这个链路组下的端口详情数据,已经查过es并且组装完成,链路组查询接口这个对这个链路下的端口做汇总,链路组导出不需要汇总,前面已经封装好了,这里可以自由选择是否汇总 List<PortReportResultVo> values = entry.getValue(); RelayLinkReport relayLinkReport = linkGroupDevPortMap.get(key); for (PortReportResultVo trDevportVO: values){ this.setRelayLinkTotalValue(relayLinkReport,trDevportVO); } } 优化这段代码
时间: 2023-12-25 18:03:21 浏览: 68
可以考虑使用Java8的Stream API来优化这段代码:
```
relayLinkAndPortMap.forEach((key, values) -> {
RelayLinkReport relayLinkReport = linkGroupDevPortMap.get(key);
values.forEach(trDevportVO -> setRelayLinkTotalValue(relayLinkReport, trDevportVO));
});
```
使用forEach遍历map,对于每个entry,直接获取链路组的报告对象,然后使用forEach遍历该entry中的端口详情数据,调用setRelayLinkTotalValue方法设置链路总值即可。这样代码更加简洁易懂,并且使用了Java8的Stream API,使代码更具可读性。
相关问题
Set<String> gatherTimeList = eSResponseList.stream() .map(DevPortReportEsDTO::getGatherTime) // 获取目标字段的值 .collect(Collectors.toSet()); // 转为列表 Map<String, DevPortReportEsDTO> reportEsDTOMap = new HashMap<>(); Map<String, List<PortReportResultVo>> relayLinkAndPortMap1 = new HashMap<>(); for (Map.Entry<String, List<TrDevportVO>> entry : relayLinkAndPortListMap.entrySet()) { String key = entry.getKey(); List<TrDevportVO> values = entry.getValue(); List<PortReportResultVo> resultVoList = new ArrayList<>(); for (String gatherTime : gatherTimeList) { for (TrDevportVO trDevportVO : values) { String tempKey = trDevportVO.getDevId() + trDevportVO.getPortName() + gatherTime; DevPortReportEsDTO reportEsDTO = reportEsDTOMap.get(tempKey); PortReportResultVo reportResultVo = new PortReportResultVo(); BeanUtil.copyProperties(trDevportVO, reportResultVo); reportResultVo.setPortBandwidth(trDevportVO.getBandwidth()); BeanUtil.copyProperties(reportEsDTO, reportResultVo); resultVoList.add(reportResultVo); } // key为链路组id+ 采集时间 relayLinkAndPortMap1.put(key + "@" + gatherTime, resultVoList); } } 优化一下这段代码
可以考虑将两个循环嵌套的部分使用 Stream 进行优化,减少循环次数,如下所示:
```
Map<String, List<PortReportResultVo>> relayLinkAndPortMap1 = relayLinkAndPortListMap.entrySet().stream()
.flatMap(entry -> gatherTimeList.stream()
.map(gatherTime -> {
String key = entry.getKey();
List<TrDevportVO> values = entry.getValue();
return values.stream()
.map(trDevportVO -> {
String tempKey = trDevportVO.getDevId() + trDevportVO.getPortName() + gatherTime;
DevPortReportEsDTO reportEsDTO = reportEsDTOMap.get(tempKey);
PortReportResultVo reportResultVo = new PortReportResultVo();
BeanUtil.copyProperties(trDevportVO, reportResultVo);
reportResultVo.setPortBandwidth(trDevportVO.getBandwidth());
BeanUtil.copyProperties(reportEsDTO, reportResultVo);
return reportResultVo;
})
.collect(Collectors.toList());
})
.map(resultVoList -> key + "@" + gatherTime, resultVoList))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
```
这样可以减少循环次数,提高代码效率。
// es查询返回的数据组成map,key为devId+portName,value为DevPortReportEsDTO对象 Map<String, DevPortReportEsDTO> reportEsDTOMap = list.stream() .collect(Collectors.toMap(reportEsDTO -> reportEsDTO.getMo_id() + reportEsDTO.getObject_id(), reportEsDTO -> reportEsDTO)); Map<String, List<PortReportResultVo>> relayLinkAndPortMap = new HashMap<>(); // 遍历链路组下包含的端口 for(Map.Entry<String, List<TrDevportVO>> entry : relayLinkAndPortListMap.entrySet()) { String key = entry.getKey(); List<TrDevportVO> values = entry.getValue(); List<PortReportResultVo> resultVoList = new ArrayList<>(); for (TrDevportVO trDevportVO: values){ String tempKey = trDevportVO.getDevId() + trDevportVO.getPortName(); DevPortReportEsDTO reportEsDTO = reportEsDTOMap.get(tempKey); PortReportResultVo reportResultVo = new PortReportResultVo(); BeanUtil.copyProperties(trDevportVO,reportResultVo); reportResultVo.setPortBandwidth(trDevportVO.getBandwidth()); BeanUtil.copyProperties(reportEsDTO,reportResultVo); resultVoList.add(reportResultVo); } relayLinkAndPortMap.put(key,resultVoList); } 优化这段代码
可以考虑使用Java 8的流式操作来简化代码,如下所示:
```
Map<String, List<PortReportResultVo>> relayLinkAndPortMap = relayLinkAndPortListMap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().stream().map(trDevportVO -> {
String tempKey = trDevportVO.getDevId() + trDevportVO.getPortName();
DevPortReportEsDTO reportEsDTO = reportEsDTOMap.get(tempKey);
PortReportResultVo reportResultVo = new PortReportResultVo();
BeanUtil.copyProperties(trDevportVO, reportResultVo);
reportResultVo.setPortBandwidth(trDevportVO.getBandwidth());
BeanUtil.copyProperties(reportEsDTO, reportResultVo);
return reportResultVo;
}).collect(Collectors.toList())
));
```
这段代码使用了`stream()`方法将`relayLinkAndPortListMap`的`entrySet()`转化为一个流,然后使用`Collectors.toMap()`方法将每个`entry`转化为对应的`key`和`value`,其中`key`和原来的代码一样,`value`使用`stream()`方法将`entry`的`value`列表转化为另一个流,然后使用`map()`方法将每个`TrDevportVO`对象转化为对应的`PortReportResultVo`对象,最后使用`collect()`方法将所有`PortReportResultVo`对象转化为一个列表。
阅读全文