JAVA - 4行代码实现省市区三级树形结构

思路

无脑把省市区单独从数据库拿出来,然后通过分组组合成一颗树。由于这不能连数据库,所以得模拟些数据。

  • 先新建个实体类
@Data
@Accessors(chain = true)
public class CityTreeVo {
     
	//父类ID
    private Integer parentId;
	//ID
    private Integer id;
	//名称
    private String name;
	//子类
    private List<CityTreeVo> child;
}

  • 新建三个list模拟下数据
//省份
 List<CityTreeVo> province = new LinkedList<>();
province.add(new CityTreeVo().setId(44000).setName("广东"));
province.add(new CityTreeVo().setId(42330).setName("黑龙江"));
//城市
List<CityTreeVo> city = new LinkedList<>();
city.add(new CityTreeVo().setParentId(44000).setId(44001).setName("肇庆市"));
city.add(new CityTreeVo().setParentId(44000).setId(44002).setName("广州市"));
city.add(new CityTreeVo().setParentId(44000).setId(44003).setName("佛山市"));
city.add(new CityTreeVo().setParentId(44000).setId(44004).setName("深圳市"));
city.add(new CityTreeVo().setParentId(42330).setId(42331).setName("哈尔滨市"));
city.add(new CityTreeVo().setParentId(42330).setId(42332).setName("绥化市"));
//区
List<CityTreeVo> area = new LinkedList<>();
area.add(new CityTreeVo().setParentId(44001).setId(4400101).setName("端州区"));
area.add(new CityTreeVo().setParentId(44001).setId(4400102).setName("高要区"));
area.add(new CityTreeVo().setParentId(44001).setId(4400103).setName("广宁县"));
area.add(new CityTreeVo().setParentId(44001).setId(4400104).setName("怀集县"));
area.add(new CityTreeVo().setParentId(44002).setId(4400201).setName("番禺区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400202).setName("天河区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400203).setName("海珠区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400204).setName("白云区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400205).setName("黄埔区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400206).setName("荔湾区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400207).setName("南沙区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400208).setName("增城区"));
area.add(new CityTreeVo().setParentId(44002).setId(4400209).setName("从化区"));
area.add(new CityTreeVo().setParentId(44003).setId(4400301).setName("禅城区"));
area.add(new CityTreeVo().setParentId(44003).setId(4400301).setName("禅城区"));
area.add(new CityTreeVo().setParentId(44003).setId(4400302).setName("三水区"));
area.add(new CityTreeVo().setParentId(44003).setId(4400303).setName("南海区"));
area.add(new CityTreeVo().setParentId(44004).setId(4400401).setName("南山区"));
area.add(new CityTreeVo().setParentId(44004).setId(4400402).setName("罗湖区"));
area.add(new CityTreeVo().setParentId(44004).setId(4400403).setName("坪山区"));
area.add(new CityTreeVo().setParentId(42331).setId(4233101).setName("道里区"));
area.add(new CityTreeVo().setParentId(42331).setId(4233102).setName("道外区"));
area.add(new CityTreeVo().setParentId(42331).setId(4233103).setName("平房区"));
area.add(new CityTreeVo().setParentId(42331).setId(4233104).setName("香坊区"));
area.add(new CityTreeVo().setParentId(42331).setId(4233105).setName("双城区"));
area.add(new CityTreeVo().setParentId(42332).setId(4233201).setName("北林区"));
  • 数据合并
//区域分组
Map<Integer, List<CityTreeVo>> areaGroup = area.stream().collect(Collectors.groupingBy(CityTreeVo::getParentId));
//城市插入已分组的区域
city.forEach(t -> {
      t.setChild(areaGroup.get(t.getId())); });
//城市分组
Map<Integer, List<CityTreeVo>> cityGroup = city.stream().collect(Collectors.groupingBy(CityTreeVo::getParentId));
//省份插入已经分组的城市
province.forEach(t ->{
      t.setChild(cityGroup.get(t.getId())); });

搞定,可以看下树形结构
JAVA - 4行代码实现省市区三级树形结构_第1张图片
有点乱?没关系,来写个有点逼格的遍历打印

public static void showTree(List<CityTreeVo> list,int deep){
     
        list.forEach( l ->{
     
            switch (deep){
     
                case 1:
                    System.out.println(l.getName());
                    break;
                case 2:
                    System.out.println("|----" + l.getName());
                    break;
                case 3:
                    System.out.println("     |----" + l.getName());
                    break;
            }
            if (l.getChild() != null){
     
                showTree(l.getChild(),deep + 1);
            }
        });
    }
 //调用
showTree(province,1);

效果:
JAVA - 4行代码实现省市区三级树形结构_第2张图片

你可能感兴趣的:(JAVA)