All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mengweijin.quickboot.framework.util.TreeUtils Maven / Gradle / Ivy

There is a newer version: 1.0.45
Show newest version
package com.github.mengweijin.quickboot.framework.util;

import cn.hutool.core.util.ReflectUtil;
import com.github.mengweijin.quickboot.framework.exception.ServerException;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Meng Wei Jin
 * @description 组装JSON格式的树结构工具类
 **/
@Slf4j
public class TreeUtils {

    /**
     * 根据父id递归查找过滤其下所有的子
     * @param list 所有数据集合
     * @param parentIds 父id数组
     * @return
     */
    public static List> filterMapByPid(List> list, String... parentIds){
        return filterMapByRootId("id", "pid", list, parentIds);
    }

    /**
     * 根据父id递归查找过滤其下所有的子
     * @param list 所有数据集合
     * @param parentIds 父id数组
     * @return
     */
    public static List> filterMapByParentId(List> list, String... parentIds){
        return filterMapByRootId("id", "parentId", list, parentIds);
    }

    /**
     * 根据父id递归查找过滤其下所有的子
     * @param idName id属性名称,如:id
     * @param parentIdName 父id属性名称,如:parentId、pid等
     * @param list 所有数据集合
     * @param parentIds 父id数组
     * @return
     */
    public static List> filterMapByRootId(String idName, String parentIdName, List> list, String... parentIds){
        List> resultList = new ArrayList<>();
        for (String parentId: parentIds){
            //  lambda表达式 根据parentId过滤
            List> filterList = list.stream()
                    .filter(map -> parentId.equals(String.valueOf(map.get(parentIdName))))
                    .collect(Collectors.toList());

            // 如果过滤到数据,添加到返回结果集合,并往下递归查找添加
            if(filterList.size() > 0){
                resultList.addAll(filterList);

                List parentIdList = new ArrayList<>(filterList.size());
                filterList.forEach(map -> parentIdList.add(String.valueOf(map.get(idName))));
                // 递归
                resultList.addAll(filterMapByRootId(idName, parentIdName, list, parentIdList.toArray(new String[parentIdList.size()])));
            }
        }

        return resultList;
    }

    public static  List filterBeanByPid(List list, String... parentIds) throws ServerException {
        return filterBeanByRootId("id", "pid", list, parentIds);
    }

    public static  List filterBeanByParentId(List list, String... parentIds) throws ServerException {
        return filterBeanByRootId("id", "parentId", list, parentIds);
    }

    /**
     * 根据父id递归查找过滤其下所有的子
     * @param idName id属性名称,如:id
     * @param parentIdName 父id属性名称,如:parentId、pid等
     * @param list 所有数据集合
     * @param parentIds 父id数组
     * @param 
     * @return
     * @throws ServerException
     */
    public static  List filterBeanByRootId(String idName, String parentIdName, List list, String... parentIds) throws ServerException {
        List resultList = new ArrayList<>();
        try{
            for (String parentId: parentIds){
                //  lambda表达式 根据parentId过滤
                List filterList = list.stream()
                        .filter(e -> parentId.equals(String.valueOf(ReflectUtil.getFieldValue(e, parentIdName))))
                        .collect(Collectors.toList());

                // 如果过滤到数据,添加到返回结果集合,并往下递归查找添加
                if(filterList.size() > 0){
                    resultList.addAll(filterList);

                    List parentIdList = new ArrayList<>(filterList.size());
                    filterList.forEach(e -> parentIdList.add(String.valueOf(ReflectUtil.getFieldValue(e, idName))));
                    // 递归
                    resultList.addAll(filterBeanByRootId(idName, parentIdName, list, parentIdList.toArray(new String[parentIdList.size()])));
                }
            }
        } catch (RuntimeException e){
            log.error(e.getMessage(), e);
            throw new ServerException(e);
        }

        return resultList;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy