TreeUtil

public class TreeUtil {

    /**
     * List转Tree
     * @param source 源数据(List)
     * @param resultList 结果数据(Tree)
     * @param root 是顶级菜单的条件
     * @param getIdFunction 获取id
     * @param getPIdFunction 获取父id
     * @param getChildFunction 获取子列表
     * @param setChildFunction 设置子列表
     * @param  类型
     * @return 结果数据(Tree)
     */
    public static  List list2Tree(List source,List resultList, Predicate root, Function getIdFunction, Function getPIdFunction, Function> getChildFunction, BiConsumer> setChildFunction){
        //
        final Map map = new HashMap<>();
        //遍历处理
        source.forEach((it)->{
            Optional.ofNullable(root).map(r -> {
                if (r.test(it)){
                    resultList.add(it);
                }
                return it;
            }).orElseGet(() -> {
                Optional.ofNullable(getPIdFunction.apply(it)).orElseGet(() -> {
                    resultList.add(it);
                    return null;
                });
                return null;
            });
            map.put(getIdFunction.apply(it),it);
        });
        //遍历处理
        source.forEach((it)->{
            map.computeIfPresent(getPIdFunction.apply(it), (k, v)->{
                Optional.ofNullable(getChildFunction.apply(v)).orElseGet(()->{
                    final List list = new ArrayList<>();
                    setChildFunction.accept(v,list);
                    return list;
                }).add(it);
                return v;
            });
        });
        return resultList;
    }

    /**
     * Tree转List
     * @param source 源数据(Tree)
     * @param resultList 结果数据(List)
     * @param getChildFunction 获取子列表
     * @param setChildFunction 设置子列表
     * @param  类型
     * @return 结果数据(List)
     */
    public static  List tree2List(List source,List resultList,Function> getChildFunction,BiConsumer> setChildFunction){
        source.forEach((it) -> {
            List childList = getChildFunction.apply(it);
            if (null != childList && 0 != childList.size()){
                tree2List(childList,resultList,getChildFunction,setChildFunction);
            }
            setChildFunction.accept(it,null);
            resultList.add(it);
        });
        return resultList;
    }

    /**
     * 在树上获取分支
     * @param source 源数据(Tree)
     * @param resultList 结果数据(List)
     * @param root 分支的条件
     * @param  类型
     * @return 结果数据(List)
     */
    public static  List getBranchFromTree(List source,List resultList,Predicate root,Function> getChildFunction,BiConsumer> setChildFunction){
        if (null == source || 0 == source.size()){
            return source;
        }
        source.forEach((it)->{
            List childList = getChildFunction.apply(it);
            if (!root.test(it)){
                getBranchFromTree(childList,resultList,root,getChildFunction,setChildFunction);
            }else {
                if (null != childList && 0 != childList.size()){
                    getBranchFromTree(childList,resultList,t -> { return true; },getChildFunction,setChildFunction);
                }
                setChildFunction.accept(it,null);
                resultList.add(it);
            }
        });
        return resultList;
    }
}

你可能感兴趣的:(TreeUtil)