组装树形结构数据功能封装

  • 前言:
  • 由于上一篇博客只针对于实体TreeNode组装树形结构,但是实际开发中,我们对应数据表会创建很多实体来承载数据,反成不能每一种实体都需要写个递归或者循环的方法来组装数据结构吧?下面我将上一拼博客中的方法再进一步封装一下,封装成一个工具类,应对与所有的实体,但是这些实体中,必须含有HashSet类型的 children 成员变量,并生成 get set 方法;必须含有String类型的 id,parentId 成员变量,并且生成get set 方法。
 /**
     * 使用递归方法建树
     * @param
     * @return
     */
    public static <T> List<T> buildTree(List<T> list) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, IntrospectionException {
        List<T> trees = new ArrayList<>();
        for (T treeNode : list) {
            Method method = treeNode.getClass().getDeclaredMethod("getParentId", null);
            Object value = method.invoke(treeNode, null);
            if ("0".equals(String.valueOf(value))) {
                trees.add(findChildren(treeNode,list));
            }
        }
        return trees;
    }
 public static <T> T findChildren(T t,List<T> treeNodes) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IntrospectionException {
        Method getIdMethod = t.getClass().getDeclaredMethod("getId");
        String id = String.valueOf(getIdMethod.invoke(t, null));
        for (T it : treeNodes) {
            Method getParentIdMethod = it.getClass().getDeclaredMethod("getParentId");
            Object parentId = getParentIdMethod.invoke(it, null);
            if (id.equals(String.valueOf(parentId))) {
                Method getChildrenMethod = t.getClass().getDeclaredMethod("getChildren");
                Object children = getChildrenMethod.invoke(t, null);
                if (null == children ) {
                    Method setChildrenMethod = t.getClass().getDeclaredMethod("setChildren",HashSet.class);
                    setChildrenMethod.invoke(t, new HashSet<T>());
                }
                ((HashSet<T>)getChildrenMethod.invoke(t, null)).add(findChildren(it, treeNodes));
            }

        }
        return t;
    }

小结:
以上就是封装的工具类,本篇博客只是学习笔记使用,大家有更好的idea欢迎交流,请喷子远离!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 中,可以使用递归算法来快速封装树形结构数据。以下是一个示例代码,展示如何使用递归算法来封装树形结构数据: ```java public class TreeUtils { /** * 将数据列表封装树形结构 * @param dataList 数据列表 * @param <T> 数据类型 * @return 树形结构 */ public static <T extends TreeNode> List<T> buildTree(List<T> dataList) { List<T> treeList = new ArrayList<>(); // 构建根节点 for (T node : dataList) { if (node.getParentId() == null || node.getParentId().equals("")) { treeList.add(node); } } // 构建子节点 for (T node : treeList) { buildChildren(node, dataList); } return treeList; } /** * 递归构建子节点 * @param parentNode 父节点 * @param dataList 数据列表 * @param <T> 数据类型 */ private static <T extends TreeNode> void buildChildren(T parentNode, List<T> dataList) { for (T node : dataList) { if (node.getParentId() != null && node.getParentId().equals(parentNode.getId())) { parentNode.addChild(node); buildChildren(node, dataList); } } } } ``` 在上面的示例代码中,buildTree() 方法用于将数据列表封装树形结构,buildChildren() 方法用于递归构建子节点。在构建树形结构时,首先需要找到根节点,然后递归构建子节点。对于每个节点,可以通过 addChild() 方法将其添加到父节点的 children 列表中。 使用上述代码,可以快速地封装树形结构数据,并且可以方便地遍历和操作树形结构数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值