有父子关系的List转树形结构

依赖


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        

业务代码

    public JSONArray areasTree() {
        List<CommonAreas> commonAreas = new ArrayList<>();
			//TODO 业务数据
            //List转树形结构
            return List2TreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(commonAreas )), "areaId", "parentId", "children");
        }
        return null;
    }

CommonAreas实体类

package com.xian.member.domain.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * <p>
 * 地区表
 * </p>
 *
 * @author devinx
 * @since 2021-07-21
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class CommonAreas implements Serializable {

    private static final long serialVersionUID=1L;

    /**
     * 地区id
     */
    private Integer areaId;

    /**
     * 地区父id
     */
    private Integer parentId;
    /**
     * 地区名称
     */
    private String areaName;

    /**
     * 地区类型 0:country,1:province,2:city,3:district
     */
    private Integer areaType;

    /**
     * 排序
     */
    private Integer sort;
}

List2TreeUtil 实体类

package com.devinx.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName List2Tree
 * @Author devin xu
 * @Description
 * @Date 2021/7/20 002015:04
 * @Version 1.0
 **/
public class List2TreeUtil {
    public static JSONArray listToTree(JSONArray arr, String id, String pid, String child) {
        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        //将数组转为Object的形式,key为数组中的id
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(id), json);
        }
        //遍历结果集
        for (int j = 0; j < arr.size(); j++) {
            //单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            //在hash中取出key为单条记录中pid的值
            String pidStr = "";
            if (null != aVal.get(pid)) {
                pidStr = aVal.get(pid).toString();
            }
            JSONObject hashVP = (JSONObject) hash.get(pidStr);
            //如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
            if (hashVP != null) {
                //检查是否有child属性
                if (hashVP.get(child) != null) {
                    JSONArray ch = (JSONArray) hashVP.get(child);
                    ch.add(aVal);
                    hashVP.put(child, ch);
                } else {
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }
            } else {
                r.add(aVal);
            }
        }
        return r;
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值