有父子关系的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;
    }

}
经验分享 程序员 微信小程序 职场和发展