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:

  1. The 'entrySet()' method is called on 'levelSubtotalDict' to get a stream of key-value pairs from the outer map.
  2. 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:
  3. The 'entry.getValue()' method is called to get the inner map for each outer key-value pair.
  4. The 'entrySet()' method is called on the inner map to get a stream of key-value pairs from the inner map.
  5. 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.
  6. Within the 'map' method, the 'scoreMap.entrySet()' method is called to get a stream of key-value pairs from the score map.
  7. 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.
  8. The 'collect' method is used to collect the stream of 'ComputeResultExportDTO' objects into a list.
  9. The resulting list of 'ComputeResultExportDTO' objects is used as the value in the new key-value pair for the inner map.
  10. 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.
  11. The resulting map is used as the value in the new key-value pair for the outer map.
  12. 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
                    ))
            ));
Transforming Nested Maps in Java: From `Map<String, Map<String, Map<String, BigDecimal>>>` to `Map<String, Map<String, List<ComputeResultExportDTO>>>`

原文地址: https://www.cveoy.top/t/topic/qEMb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录