Iterating a list inside a map of map using java 8 streams

Loui :

I have a map that contains date as key and (another map of string as key and list as the value) as value. Map<LocalDate, Map<String, List<FlexiServer>>>, I want to populate another map that has String as a key and double as a value. The contents of FlexiServer are

public class FlexiServer {
    private String serverNumber;
    private Double quantity;
    private Integer level;
    private String parentServer;
}

So, basically I want to iterate from first the exterior map to get the internal map and then iterate the internal map to get the list of FlexiServers and populate the new map having server number as key and list of quantities as values. How can I do this using java 8 streams?

I tried with for loop, but I will like to replace it using Java streams.

if(data.isPresent()) {
            for(Map.Entry<LocalDate, Map<String, List<FlexiServer>>> entry : data.get().entrySet()) {
                for(Map.Entry<String, List<FlexiServer>> innerEntry : entry.getValue().entrySet()) {
                    for(FlexiServer dto : innerEntry.getValue()) {
                        dailyRequirements.computeIfAbsent(dto.getserverNumber(),
                            k -> new ArrayList<>()).add(dto.getQuantity());
                    }
                }
            }
        }
Deadpool :

Just get the values from the outer map and inner map and then use Collectors.groupingby

Map<String, List<Double>> result = map.values()
                                      .stream()
                                      .map(Map::values)
                                      .flatMap(Collection::stream)
                                      .flatMap(List::stream)
            .collect(Collectors.groupingBy(FlexiServer::getServerNumber, Collectors.mapping(FlexiServer::getQuantity, Collectors.toList())));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26970&siteId=1