java实现树状结构

controller 

    @GetMapping("/getDivisionInfo")
    public AjaxResult getDivisionInfo() {
        List<BzCodSSysAdminDivision> dList = bzCodSSysAdminDivisionService.list();
        Map<String, List<TreeNode>> map=new HashMap<String, List<TreeNode>>();
        TreeNode node=null;
        Set<String> idSet=new HashSet<>();
        for (BzCodSSysAdminDivision bzCodSSysAdminDivision : dList) {
            if(!idSet.contains(bzCodSSysAdminDivision.getPkSadId())) {
                node = new TreeNode();
                node.setId(bzCodSSysAdminDivision.getPkSadId().toString());
                node.setName(bzCodSSysAdminDivision.getName());
                node.setCode(bzCodSSysAdminDivision.getCode());
                if(map.get(bzCodSSysAdminDivision.getParentId().toString())==null) {
                    map.put(bzCodSSysAdminDivision.getParentId().toString(), new ArrayList<>());
                }
                map.get(bzCodSSysAdminDivision.getParentId().toString()).add(node);
                idSet.add(bzCodSSysAdminDivision.getPkSadId().toString());
            }
        }
        Iterator<String> it=map.keySet().iterator();
        String id=null;
        while(it.hasNext()) {
            id = it.next();
            //根据id(键)取到一个集合
            List<TreeNode> childeNodes=map.get(id);
            for (TreeNode treeNode : childeNodes) {
                if(map.get(treeNode.getId())!=null) {
                    //将集合塞进对象
                    treeNode.getChildren().addAll(map.get(treeNode.getId()));
                    System.out.println(treeNode.getChildren().size());
                }
            }
        }
        return AjaxResult.success(JSONObject.toJSON(map.get("0")));
    }

实体类

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
​
@Data
@ApiModel("全国行政区划信息(到县级)对象 ")
public class BzCodSSysAdminDivision extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 主键 */
    @ApiModelProperty("主键")
    private Long pkSadId;

    /** 行政区划编码(到县只有6位,前两位代表省,前4位代表市,前6位代表区县) */
    @ApiModelProperty("行政区划编码")
    @Excel(name = "行政区划编码")
    private String code;

    /** 行政区划名称 */
    @ApiModelProperty("行政区划名称")
    @Excel(name = "行政区划名称")
    private String name;

    /** 上级行政区划主键id(省级父id为0) */
    @ApiModelProperty("上级行政区划主键id")
    @Excel(name = "上级行政区划主键id", readConverterExp = "省=级父id为0")
    private Long parentId;

    /** 行政区划名称首字母 */
    @ApiModelProperty("行政区划名称首字母")
    @Excel(name = "行政区划名称首字母")
    private String firstLetter;

    /** 行政区划级别 0:省级、1:市级、 2:区县级别 */
    @ApiModelProperty("行政区划级别 0:省级、1:市级、 2:区县级别")
    @Excel(name = "行政区划级别 0:省级、1:市级、 2:区县级别")
    private Long level;

    /** 行政区大区分类 */
    @ApiModelProperty("行政区大区分类")
    @Excel(name = "行政区大区分类")
    private String largeArea;

    /** 经度 */
    @ApiModelProperty("经度")
    @Excel(name = "经度")
    private BigDecimal lon;

    /** 纬度 */
    @ApiModelProperty("纬度")
    @Excel(name = "纬度")
    private BigDecimal lat;
}

​
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TreeNode   implements Serializable {

    private String id;

    private String name;

    private String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    private List<TreeNode> children=new ArrayList<TreeNode>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

}
 结果
{
  "msg": "操作成功",
  "code": 200,
  "data": [
    {
      "code": "370000000000",
      "children": [
        {
          "code": "371600000000",
          "children": [
            {
              "code": "371602000000",
              "children": [],
              "name": "滨城区",
              "id": "921"
            },
            {
              "code": "371621000000",
              "children": [],
              "name": "惠民县",
              "id": "922"
            },
            {
              "code": "371623000000",
              "children": [],
              "name": "无棣县",
              "id": "923"
            },
            {
              "code": "371622000000",
              "children": [],
              "name": "阳信县",
              "id": "2451"
            },
            {
              "code": "371625000000",
              "children": [],
              "name": "博兴县",
              "id": "2452"
            },
            {
              "code": "371603000000",
              "children": [],
              "name": "沾化区",
              "id": "3024"
            },
            {
              "code": "371681000000",
              "children": [],
              "name": "邹平市",
              "id": "3025"
            }
          ],
          "name": "滨州市",
          "id": "846"
        }
      ],
      "name": "山东省",
      "id": "12"
    }
  ]
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值