Java 3 级嵌套列表到 3 级嵌套映射

Java 3 level nested list to 3 level nested map

我有一个列表的列表。如何转换为地图的地图?

Public class Country{
  private String name;
  private List<State> states;
}

Public class State{
  private String name;
  private List<City> cities
}

Public class City{
  private String name;
  private List<Town> towns
}

public class Town{
  private String name;
  private Integer population;
}

我想要这些格式的城市和城镇的州地图:

  1. 使用整个对象作为键: 第一级:州 -> 城市。二级:城市 -> 城镇。

  2. 使用名称作为键: 第一级:州名->城市名。第二层:城市名->城镇

请帮忙,我一直在尝试使用流来完成此操作,但到目前为止无法做到。

不要忘记将 getter、setter 和构造函数添加到 类

Map<String, Map<String, Map<String, Map<String, Integer>>>> map = countries.stream()
.collect(Collectors.toMap(Country::getName, country -> country.getStates().stream()
.collect(Collectors.toMap(State::getName, state -> state.getCities().stream()
.collect(Collectors.toMap(City::getName, city -> city.getTowns().stream()
.collect(Collectors.toMap(Town::getName, Town::getPopulation))))))));

所以,我使用了来自@BuildSlayer 的答案的输入值

class Solution {
    public static void main(String[] args) {
        List<Country> countries = Collections.singletonList(new Country("CountryA", Arrays.asList(
                new State("StateA", Arrays.asList(
                        new City("cityA", Arrays.asList(
                                new Town("townA", 4),
                                new Town("townB", 5),
                                new Town("townC", 42),
                                new Town("townD", 67)
                        )),
                        new City("cityB", Arrays.asList(
                                new Town("townE", 65),
                                new Town("townF", 14),
                                new Town("townG", 24),
                                new Town("townH", 33)
                        )))),
                new State("StateB", Arrays.asList(
                        new City("cityC", Arrays.asList(
                                new Town("townI", 9),
                                new Town("townJ", 5),
                                new Town("townK", 12),
                                new Town("townL", 4)
                        )),
                        new City("cityD", Arrays.asList(
                                new Town("townM", 5),
                                new Town("townN", 7),
                                new Town("townO", 24),
                                new Town("townP", 6)
                        )))))));
        Map<String, Map<String, Map<String, Map<String, Integer>>>> map = countries.stream().collect(Collectors.toMap(Country::getName, country -> country.getStates().stream().collect(Collectors.toMap(State::getName, state -> state.getCities().stream().collect(Collectors.toMap(City::getName, city -> city.getTowns().stream().collect(Collectors.toMap(Town::getName, Town::getPopulation))))))));
        System.out.println(map);
    }
}

class Country {
    String name;
    List<State> states;

    public Country(String name, List<State> states) {
        this.name = name;
        this.states = states;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<State> getStates() {
        return states;
    }

    public void setStates(List<State> states) {
        this.states = states;
    }
}

class State {
    String name;
    List<City> cities;

    public State(String name, List<City> cities) {
        this.name = name;
        this.cities = cities;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<City> getCities() {
        return cities;
    }

    public void setCities(List<City> cities) {
        this.cities = cities;
    }
}

class City {
    String name;
    List<Town> towns;

    public City(String name, List<Town> towns) {
        this.name = name;
        this.towns = towns;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Town> getTowns() {
        return towns;
    }

    public void setTowns(List<Town> towns) {
        this.towns = towns;
    }
}

class Town {
    private String name;
    private Integer population;

    public Town(String name, Integer population) {
        this.name = name;
        this.population = population;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPopulation() {
        return population;
    }

    public void setPopulation(Integer population) {
        this.population = population;
    }
}

输出为:

{CountryA={StateA={cityA={townD=67, townB=5, townC=42, townA=4}, cityB={townH=33, townF=14, townG=24, townE=65}}, StateB={cityC={townI=9, townL=4, townJ=5, townK=12}, cityD={townP=6, townN=7, townO=24, townM=5}}}}

您可以进行如下操作:

Map<State, Map<City, List<Town>>> stateToCitiesToTowns =
        country.getStates().stream()
                .collect(Collectors.toMap(state -> state, state -> state.getCities().stream()
                        .collect(Collectors.toMap(city -> city, City::getTowns))));

System.out.println(stateToCitiesToTowns);

Map<String, Map<String, List<String>>> stateToCitiesToTownNames =
        country.getStates().stream()
                .collect(Collectors.toMap(State::getName, state -> state.getCities().stream()
                        .collect(Collectors.toMap(City::getName, city -> city.getTowns().stream()
                                .map(Town::getName)
                                .collect(Collectors.toList())))));

System.out.println(stateToCitiesToTownNames);

输入:

Country country = new Country("CountryA", Arrays.asList(
        new State("StateA", Arrays.asList(
                new City("cityA", Arrays.asList(
                        new Town("townA", 4),
                        new Town("townB", 5),
                        new Town("townC", 42),
                        new Town("townD", 67)
                )),
                new City("cityB", Arrays.asList(
                        new Town("townE", 65),
                        new Town("townF", 14),
                        new Town("townG", 24),
                        new Town("townH", 33)
                )))),
        new State("StateB", Arrays.asList(
                new City("cityC", Arrays.asList(
                        new Town("townI", 9),
                        new Town("townJ", 5),
                        new Town("townK", 12),
                        new Town("townL", 4)
                )),
                new City("cityD", Arrays.asList(
                        new Town("townM", 5),
                        new Town("townN", 7),
                        new Town("townO", 24),
                        new Town("townP", 6)
                ))))));

输出(很长,因为键和值都有相同的列表):

{State(name=StateB, cities=[City(name=cityC, towns=[Town(name=townI, population=9), Town(name=townJ, population=5), Town(name=townK, population=12), Town(name=townL, population=4)]), City(name=cityD, towns=[Town(name=townM, population=5), Town(name=townN, population=7), Town(name=townO, population=24), Town(name=townP, population=6)])])={City(name=cityC, towns=[Town(name=townI, population=9), Town(name=townJ, population=5), Town(name=townK, population=12), Town(name=townL, population=4)])=[Town(name=townI, population=9), Town(name=townJ, population=5), Town(name=townK, population=12), Town(name=townL, population=4)], City(name=cityD, towns=[Town(name=townM, population=5), Town(name=townN, population=7), Town(name=townO, population=24), Town(name=townP, population=6)])=[Town(name=townM, population=5), Town(name=townN, population=7), Town(name=townO, population=24), Town(name=townP, population=6)]}, State(name=StateA, cities=[City(name=cityA, towns=[Town(name=townA, population=4), Town(name=townB, population=5), Town(name=townC, population=42), Town(name=townD, population=67)]), City(name=cityB, towns=[Town(name=townE, population=65), Town(name=townF, population=14), Town(name=townG, population=24), Town(name=townH, population=33)])])={City(name=cityB, towns=[Town(name=townE, population=65), Town(name=townF, population=14), Town(name=townG, population=24), Town(name=townH, population=33)])=[Town(name=townE, population=65), Town(name=townF, population=14), Town(name=townG, population=24), Town(name=townH, population=33)], City(name=cityA, towns=[Town(name=townA, population=4), Town(name=townB, population=5), Town(name=townC, population=42), Town(name=townD, population=67)])=[Town(name=townA, population=4), Town(name=townB, population=5), Town(name=townC, population=42), Town(name=townD, population=67)]}}

输出:

{
 StateA=
        {
         cityA=[townA, townB, townC, townD],
         cityB=[townE, townF, townG, townH]
        }, 
 StateB={
         cityC=[townI, townJ, townK, townL], 
         cityD=[townM, townN, townO, townP]
        }
}