Java 通用递归树形结构

1、tree抽象基类

public abstract class BaseTreeNode implements Serializable {

    /**
     * 获取当前code
     * @return
     */
    public abstract String obtainCode();

    /**
     * 获取父code
     * @return
     */
    public abstract String obtainParentCode();

    /**
     * 添加到子集
     * @param node
     */
    public abstract void add(BaseTreeNode node);
}

2、通用实现类Tree(自定义类只要实现抽象基类就可以)

@Data
public class TreeNode<T> extends BaseTreeNode {

    private static final long serialVersionUID = -1586013635569434864L;
    /**
     * 节点code
     */
    private T code;

    /**
     * 节点名
     */
    private String name;

    /**
     * 父节点code
     */
    @JsonIgnore
    private T parentCode;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeNode> children = new ArrayList<>();

    public TreeNode(){}

    public TreeNode(T code,String name,T parentCode){
        this.code = code;
        this.name = name;
        this.parentCode = parentCode;
    }

    @Override
    public String obtainCode() {
       return String.valueOf(this.code);
    }

    @Override
    public String obtainParentCode() {
        return String.valueOf(this.parentCode);
    }

    @Override
    public void add(BaseTreeNode node) {
        this.children.add((TreeNode) node);
    }
}

3、tree帮助类

@UtilityClass
public class TreeUtil {

    /**
     * 获取层级列表
     * @param voList
     * @return
     */
    public List<BaseTreeNode> build(List<BaseTreeNode> voList){
        List<BaseTreeNode> returnList = new ArrayList<>();
        List<String> tempList = new ArrayList<>();
        for (BaseTreeNode t : voList) {
            tempList.add(t.obtainCode());
        }

        for (Iterator<BaseTreeNode> iterator = voList.iterator();iterator.hasNext();){
            BaseTreeNode vo = iterator.next();
            if(!tempList.contains(vo.obtainParentCode())){
                returnList.add(findChildren(voList,vo));
            }
        }
        if(returnList.isEmpty()){
            returnList = voList;
        }
        return returnList;
    }

    private static BaseTreeNode findChildren(List<BaseTreeNode> voList, BaseTreeNode vo) {
        Iterator<BaseTreeNode> it = voList.iterator();
        while (it.hasNext()){
            BaseTreeNode node = it.next();
            String parentCode = node.obtainParentCode();
            if(StringUtils.isNotEmpty(parentCode) && vo.obtainCode().equalsIgnoreCase(parentCode)){
                if(hasChild(voList,node)){
                    node = findChildren(voList,node);
                }
                vo.add(node);
            }
        }
        return vo;
    }

    private static boolean hasChild(List<BaseTreeNode> voList, BaseTreeNode node) {
        return voList.stream().anyMatch(vo -> StringUtils.isNotEmpty(vo.obtainParentCode()) && node.obtainCode().equalsIgnoreCase(vo.obtainParentCode()));
    }
}

4、应用

    @Override
    public List<BaseTreeNode> buildTree(List<DictVo> voList) throws Exception {
        List<BaseTreeNode> treeNodes = voList.stream().map(vo -> new TreeNode(vo.getCode(), vo.getName(), vo.getParentCode())).collect(Collectors.toList());
        return TreeUtil.build(treeNodes);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值