Transforming Nested Maps in Java: From `Map<String, Map<String, Map<String, BigDecimal>>>` to `Map<String, Map<String, List<ComputeResultExportDTO>>>`
This code takes a nested map called 'levelSubtotalDict' and transforms it into a new map with a different structure.
The input map 'levelSubtotalDict' has the following structure:
'Map<String, Map<String, Map<String, BigDecimal>>>'
The output map has the following structure:
'Map<String, Map<String, List
The transformation is done using Java streams and the 'collect' method. Here's how it works:
- The 'entrySet()' method is called on 'levelSubtotalDict' to get a stream of key-value pairs from the outer map.
- The 'collect' method is used to collect the stream into a new map. The key remains the same, but the value is transformed using the following steps:
- The 'entry.getValue()' method is called to get the inner map for each outer key-value pair.
- The 'entrySet()' method is called on the inner map to get a stream of key-value pairs from the inner map.
- The 'map' method is used to transform each inner key-value pair into a new key-value pair. The key is the same as the inner key, and the value is a list of 'ComputeResultExportDTO' objects.
- Within the 'map' method, the 'scoreMap.entrySet()' method is called to get a stream of key-value pairs from the score map.
- The 'map' method is used to transform each score key-value pair into a new 'ComputeResultExportDTO' object. The properties of this object are set based on the key-value pair and additional information from the 'empNameAndCode' object.
- The 'collect' method is used to collect the stream of 'ComputeResultExportDTO' objects into a list.
- The resulting list of 'ComputeResultExportDTO' objects is used as the value in the new key-value pair for the inner map.
- The 'collect' method is used to collect the stream of inner key-value pairs into a new map. The key remains the same, and the value is the list of 'ComputeResultExportDTO' objects.
- The resulting map is used as the value in the new key-value pair for the outer map.
- Finally, the outer map is collected and returned.
final Map<String, Map<String, Map<String, BigDecimal>>> levelSubtotalDict
return levelSubtotalDict.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().entrySet().stream()
.map(levelEntry -> {
String levelName = levelEntry.getKey();
Map<String, BigDecimal> scoreMap = levelEntry.getValue();
List<ComputeResultExportDTO> resultList = scoreMap.entrySet().stream()
.map(scoreEntry -> {
String assessScore = scoreEntry.getKey();
BigDecimal totalScore = scoreEntry.getValue();
ComputeResultExportDTO computeResultExportDTO = new ComputeResultExportDTO();
computeResultExportDTO.setEmpCode(empNameAndCode.getEmpCode());
computeResultExportDTO.setEmpName(empNameAndCode.getEmpName());
computeResultExportDTO.setLevelName('级别小计');
computeResultExportDTO.setLevelGroup(levelName);
computeResultExportDTO.setAssessScore(new BigDecimal(assessScore));
computeResultExportDTO.setTotalScore(totalScore);
return computeResultExportDTO;
})
.collect(Collectors.toList());
return new AbstractMap.SimpleEntry<>(levelName, resultList);
})
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue
))
));
原文地址: https://www.cveoy.top/t/topic/qEMb 著作权归作者所有。请勿转载和采集!