java list转树结构工具类,父节点的值不固定

搬运 java list转树结构工具类,父节点的值不固定_编个程的博客-CSDN博客

 

package com.borgrise.erp.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Classname TreeUtil
 * @Description
 * @Date 2023/8/2 13:47
 * @Created by jyl
 */
public class TreeUtil {
    /**
     * list转为树状结构
     *
     * @param dataList 需要转化的数据
     * @param id       数据唯一的标识键值
     * @param pid      父id唯一标识键值
     * @param child    子节点键值
     * @param       参数类型实体
     * @return List
     */
    public static  List listToTree(List dataList, String id, String pid, String child) {
        if (CollectionUtils.isEmpty(dataList)) {
            return new ArrayList<>();
        }
        JSONArray arr = JSONArray.parseArray(JSON.toJSONString(dataList));
        JSONArray result = new JSONArray();
        JSONObject dataObject = new JSONObject();
        // 将数组转为Object的形式,key为数组中的id
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = arr.getJSONObject(i);
            dataObject.put(json.getString(id), json);
        }
        for (int i = 0; i < arr.size(); i++) {
            JSONObject data = arr.getJSONObject(i);
            // 获取父节点
            JSONObject parentObject = dataObject.getJSONObject(data.getString(pid));
            // 如果记录的pid存在,则说明它有父节点,将数据添加到父节点的子节点集合中
            if (parentObject == null) {
                result.add(data);
            } else {
                //检查是否有child属性
                JSONArray childList = parentObject.getJSONArray(child);
                if (childList == null) {
                    childList = new JSONArray();
                    childList.add(data);
                    parentObject.put(child, childList);
                } else {
                    childList.add(data);
                }
            }
        }
        Type listType = new TypeReference>() {}.getType();
        return JSON.parseObject(result.toJSONString(), listType);
    }

    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("id", "t1");
        map.put("parentId", "t0");
        map.put("name", "tom");

        Map map1 = new HashMap();
        map1.put("id", "t2");
        map1.put("parentId", "t1");
        map1.put("name", "cat");

        Map map2 = new HashMap();
        map2.put("id", "t3");
        map2.put("parentId", "t2");
        map2.put("name", "jerry");

        Map map3 = new HashMap();
        map3.put("id", "t4");
        map3.put("parentId", "t0");
        map3.put("name", "jeck");

        List list = new ArrayList();
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        List result = listToTree(list, "id", "parentId", "children");
        System.out.println(JSON.toJSONString(result));
    }
}

你可能感兴趣的:(java,list)