Java根据省市区字符串数组,拆分成多级嵌套List


要求:根据省市区字符串数组,拆分成多级嵌套List

入参格式:str = {"广东-深圳-南山-新安街道","广东-深圳-宝安","广东-东莞-南城","湖南-长沙-开福"}

 String [] str={"广东-深圳-南山-新安街道","广东-深圳-宝安","广东-东莞-南城","湖南-长沙-开福"};

返回:嵌套List

[
    {
        "name":"广东",
        "cityList":[
            {
                "name":"深圳",
                "cityList":[
                    {
                        "name":"南山",
                        "cityList":[

                        ],
                        "indexMap":{

                        }
                    },
                    {
                        "name":"宝安",
                        "cityList":[

                        ],
                        "indexMap":{

                        }
                    }
                ],
                "indexMap":{
                    "南山":0,
                    "宝安":1
                }
            },
            {
                "name":"东莞",
                "cityList":[
                    {
                        "name":"南城",
                        "cityList":[

                        ],
                        "indexMap":{

                        }
                    }
                ],
                "indexMap":{
                    "南城":0
                }
            }
        ],
        "indexMap":{
            "东莞":1,
            "深圳":0
        }
    },
    {
        "name":"湖南",
        "cityList":[
            {
                "name":"长沙",
                "cityList":[
                    {
                        "name":"开福",
                        "cityList":[

                        ],
                        "indexMap":{

                        }
                    }
                ],
                "indexMap":{
                    "开福":0
                }
            }
        ],
        "indexMap":{
            "长沙":0
        }
    }
]

上代码

public class City{
        public String name = "";
        public List<City> cityList = new ArrayList<>();
        public Map<String,Integer> indexMap = new HashMap<>();

        public City(String name) {
            this.name = name;
        }
    }

    @RequestMapping("city")
    public AjaxResult City() {
        String [] str={"广东-深圳-南山-新安街道","广东-深圳-宝安","广东-东莞-南城","湖南-长沙-开福"};
        List<City> pList = new ArrayList<>();
        Map<String,Integer> pMap = new HashMap<>();
        for (String s : str){
            String[] citys = s.split("-");
            List<City> list = pList;
            Map<String,Integer> map = pMap;
            City city;
            int num = 0;
            do{ if (map.containsKey(citys[num])){
                    city = list.get(map.get(citys[num]));
                }else{
                    list.add(city = new City(citys[num]));
                    map.put(citys[num],list.size()-1);
                }
                list = city.cityList; map = city.indexMap;
            }while (++num < citys.length);
        }
        return AjaxResult.success(pList);
    }

优雅实现。 欢迎大家在评论区讨论,提示问题或者指导。 欢迎提出更优解方案,互相学习。

Java
Linux
  • 作者:remember(联系作者)
  • 发表时间:2022-04-02 18:33
  • 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
  • 公众号转载:请在文末添加作者公众号二维码
  • 评论