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()) )); Map.Entry::getKey这块还可以怎么写
时间: 2023-12-31 19:04:23 浏览: 69
`Map.Entry::getKey` 是一个方法引用,它引用了 `Map.Entry` 接口的 `getKey()` 方法。如果你想要避免使用方法引用,你可以使用 Lambda 表达式来代替它,如下所示:
```
entry -> entry.getKey()
```
这段 Lambda 表达式与 `Map.Entry::getKey` 产生的效果是相同的。
相关问题
// 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`对象转化为一个列表。
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));
```
这样可以减少循环次数,提高代码效率。
阅读全文