for(Map.Entry<String, List<TrDevportVO>> entry : relayLinkAndPortListMap.entrySet()) { String key = entry.getKey(); List<TrDevportVO> values = entry.getValue(); List<DevPortReportEsDTO> esDTOS = new ArrayList<>(); for (TrDevportVO trDevportVO: values){ String tempKey = trDevportVO.getDevId() + trDevportVO.getPortName(); DevPortReportEsDTO reportEsDTO = reportEsDTOMap.get(tempKey); esDTOS.add(reportEsDTO); relayLinkAndPortListMap1.put(key,esDTOS); } System.out.println("Key: " + key); System.out.println("Value: " + values); } 优化一下这段代码
时间: 2023-12-10 10:40:53 浏览: 181
可以尝试使用Java8的Stream API对代码进行优化,如下所示:
```
relayLinkAndPortListMap.forEach((key, values) -> {
List<DevPortReportEsDTO> esDTOS = values.stream()
.map(trDevportVO -> reportEsDTOMap.get(trDevportVO.getDevId() + trDevportVO.getPortName()))
.collect(Collectors.toList());
relayLinkAndPortListMap1.put(key, esDTOS);
});
```
使用Stream API可以避免显式的循环,简化代码,提高可读性。
相关问题
for(Map.Entry<String, List<TrDevportVO>> entry : relayLinkAndPortListMap.entrySet()) { String key = entry.getKey(); List<TrDevportVO> values = entry.getValue(); List<DevPortReportEsDTO> esDTOS = new ArrayList<>(); for (TrDevportVO trDevportVO: values){ String tempKey = trDevportVO.getDevId() + trDevportVO.getPortName(); DevPortReportEsDTO reportEsDTO = reportEsDTOMap.get(tempKey); esDTOS.add(reportEsDTO); } relayLinkAndPortListMap1.put(key,esDTOS); System.out.println("Key: " + key); System.out.println("Value: " + values); } 优化这段代码
可以尝试使用 Java 8 的 stream API 来进行优化,如下所示:
```
Map<String, List<DevPortReportEsDTO>> relayLinkAndPortListMap1 = relayLinkAndPortListMap.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue()
.stream()
.map(trDevportVO -> reportEsDTOMap.get(trDevportVO.getDevId() + trDevportVO.getPortName()))
.collect(Collectors.toList())
));
relayLinkAndPortListMap1.forEach((key, value) -> {
System.out.println("Key: " + key);
System.out.println("Value: " + value);
});
```
这样可以通过流式处理来避免显式的循环,使代码更加简洁易读。
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));
```
这样可以减少循环次数,提高代码效率。
阅读全文