函数式编程 + 接口+泛型 实现通用 树状数组(项目必备)

本文介绍了如何使用Java编程语言创建一个TreeNodeUtil类,用于构建树形结构,并通过实例演示了如何设置根节点判断函数和节点关系判断,以及如何通过传入的消费者接口来构建完整的树结构。博客以处理 UnitsBO 对象为例,展示了如何运用这些工具进行数据组织。
摘要由CSDN通过智能技术生成

废话不多说了,具体实现原来直接上博主的以往博客
代码地址: https://gitee.com/baichen9187/treeUtil

代码一

package com.items.Tree;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class TreeNodeUtil<E> {
    
    private List<E> boList;

    private Function<E, Boolean> RootNodeFunc;

    private IsParent<E> NodeFunc;

    public TreeNodeUtil(final List<E> boList) {
        this.boList = boList;
    }

    public void setRootNodeFunc(Function<E, Boolean> rootNodeFunc) {
        this.RootNodeFunc = rootNodeFunc;
    }

    public void setNodeFunc(IsParent<E> nodeFunc) {
        this.NodeFunc = nodeFunc;
    }

    /**
     * 获取根节点的列表
     * @param function
     * @return
     */
    private List<E> getRootNode(final Function<E, Boolean> function) {
        final List<E> rootMenuLists = new ArrayList<>();
        for (E menuNode : boList) {
            if (function.apply(menuNode)){
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

    /**
     * 建立树形结构
     * @return  List<E>
     */
    public List<E> buildTree(final Customer<E> consumer) {
        final List<E> treeMenus = new ArrayList<>();
        for (E e : getRootNode(RootNodeFunc)) {
            e = buildChildrenTree(e,consumer,NodeFunc);
            treeMenus.add(e);
        }
        return treeMenus;
    }

    /**
     * 递归,建立子树形结构
     * @param pNode
     * @return E
     */
    private E buildChildrenTree(final E pNode, final Customer<E> consumer, final IsParent<E> function) {
        final List<E> childrenMenus = new ArrayList<E>();
        for (E e : boList) {
            if (function.access(pNode,e)) {
                childrenMenus.add(buildChildrenTree(e,consumer,function));
            }
        }
        consumer.access(pNode, childrenMenus);
        return pNode;
    }
}

 	@Resource
    private UnitsMapper unitsMapper;

    public void test() {
        List<UnitsBO> list = unitsMapper.selectList(
                new LambdaQueryWrapper<>());
        TreeNodeUtil<UnitsBO> treeNodeUtil = new TreeNodeUtil<>(list);
        treeNodeUtil.setRootNodeFunc((bo) -> bo.getParentId() == null);
        treeNodeUtil.setNodeFunc((bo, parent) -> bo.getId().equals(parent.getParentId()));
        List<UnitsBO> tree = treeNodeUtil.buildTree(UnitsBO::setChildren);
        System.out.println(tree);
    }

代码二

package com.items.TreeUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;


public class ITreeUtilImpl<E,R> implements ITreeUtil<E,R> {

    /**
     * 获取父节点的方法
     */
    private Function<E, R> parentFunc;
    /**
     * 获取节点的方法
     */
    private Function<E, R> codeFunc;
    /**
     * 设置子节点的方法
     */
    private Customer<E> consumer;
    /**
     * 获取子组织列表的方法
     */
    private Function<E, List<E>> listFunc;
    /**
     * 源数据
     */
    private List<E> boList;

    public ITreeUtilImpl(){}

    public ITreeUtilImpl(List<E> list) {
        this.boList = list;
    }

    public List<E> getBoList() {
        return boList;
    }

    public void setBoList(List<E> boList) {
        this.boList = boList;
    }

    public void setListFunc(Function<E, List<E>> listFunc) {
        this.listFunc = listFunc;
    }

    public void setParentFunc(Function<E, R> parentFunc) {
        this.parentFunc = parentFunc;
    }

    public void setCodeFunc(Function<E, R> codeFunc) {
        this.codeFunc = codeFunc;
    }

    public void setConsumer(Customer<E> consumer) {
        this.consumer = consumer;
    }

    @Override
    public List<E> getTreeList(final List<E> eList) {
        final List<E> list = new ArrayList<>();
        for (E e : eList) {
            list.add(this.getTree(e, boList));
        }
        return list;
    }

    @Override
    public E getTree(E e, List<E> boList) {
        //所有节点
        final Map<R, E> map = new HashMap<>();
        final Map<R, List<E>> eMap = new HashMap<>();
        for (E bo : boList) {
            R ParentCode = parentFunc.apply(bo);
            R code = codeFunc.apply(bo);
            if (!eMap.containsKey(ParentCode)) {
                eMap.put(ParentCode, new ArrayList<>());
            }
            map.put(code, bo);
            eMap.get(ParentCode).add(bo);
        }
//        e = getTreeGen(map, e);
        this.getSubordinate(eMap, e, listFunc);
        return e;
    }


    private void getSubordinate(Map<R, List<E>> boMap, E e, Function<E, List<E>> listFunc) {
        R code = codeFunc.apply(e);
        if (boMap.containsKey(code)) {   // 从map中查询子组织列表
            consumer.access(e, boMap.get(code));     //设置bo的子组织
            for (E e1 : listFunc.apply(e)) {    // 遍历子组织列表
                this.getSubordinate(boMap, e1, listFunc);
            }
        }

    }

    @Override
    public E getTreeGen(final Map<R, E> map, E bo) {

        while (true) {
            R ParentCode = parentFunc.apply(bo);
            if (ParentCode == null || ParentCode == "") {
                break;
            }
            final E e = map.get(ParentCode);
            if (e != null) {
                bo = e;
            } else {
                break;
            }
        }
        return bo;
    }
}


demo

package com.items;


import com.items.TreeUtil.ITreeUtilImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class TreeDemo {

    static List<Test> testList = new ArrayList<Test>(){{
        add(new Test("1","一",null));
        add(new Test("2","一",null));
        add(new Test("3","一",null));
        add(new Test("1.1","一","1"));
        add(new Test("1.2","一","1"));
        add(new Test("1.1.1","一","1.1"));
        add(new Test("2.1","一","2"));
    }};

    static class Test{
        String id;
        String name;
        String Pid;
        List<Test> children;

        public Test(String id, String name, String pid) {
            this.id = id;
            this.name = name;
            Pid = pid;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPid() {
            return Pid;
        }

        public void setPid(String pid) {
            Pid = pid;
        }

        public List<Test> getChildren() {
            return children;
        }

        public void setChildren(List<Test> children) {
            this.children = children;
        }

        @Override
        public String toString() {
            return "Test{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", Pid='" + Pid + '\'' +
                    ", children=" + children +
                    '}';
        }
    }


    public static void main(String[] args) {

        ITreeUtilImpl<Test, String> treeUtil = new ITreeUtilImpl<>();
        treeUtil.setBoList(testList);
        treeUtil.setCodeFunc(Test::getId);
        treeUtil.setParentFunc(Test::getPid);
        treeUtil.setListFunc(Test::getChildren);
        treeUtil.setConsumer(Test::setChildren);
        List<Test> trees = treeUtil.getTreeList(testList.stream().filter(item-> item.getPid() == null).collect(Collectors.toList())
                );
        System.out.println(trees);
    }

    
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值