快捷搜索: 王者荣耀 脱发

使用hutool获取所有下级节点

@Slf4j
public class MainTest {
          
   

    public static void main(String[] args) throws IOException {
          
   
        List<Tree<Long>> list = new ArrayList<>();

        Tree<Long> t1 = new Tree<>();
        t1.setId(1L);
        t1.setParentId(0L);
        t1.setName("01");
        list.add(t1);

        Tree<Long> t11 = new Tree<>();
        t11.setId(11L);
        t11.setParentId(1L);
        t11.setName("11");
        list.add(t11);

        Tree<Long> t111 = new Tree<>();
        t111.setId(111L);
        t111.setParentId(11L);
        t111.setName("t111");
        list.add(t111);

        Tree<Long> t12 = new Tree<>();
        t12.setId(12L);
        t12.setParentId(1L);
        t12.setName("12");
        list.add(t12);

        Tree<Long> t1201 = new Tree<>();
        t1201.setId(1201L);
        t1201.setParentId(12L);
        t1201.setName("1201");
        list.add(t1201);

        Tree<Long> t1202 = new Tree<>();
        t1202.setId(1202L);
        t1202.setParentId(12L);
        t1202.setName("1202");
        list.add(t1202);

        Tree<Long> t2 = new Tree<>();
        t2.setId(2L);
        t2.setName("02");
        t2.setParentId(0L);
        list.add(t2);

        Tree<Long> t201 = new Tree<>();
        t201.setId(201L);
        t201.setName("201");
        t201.setParentId(2L);
        list.add(t201);

        List<Tree<Long>> treeList = TreeUtil.build(list, 0L, (treeNode, tree) -> {
          
   
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setWeight(treeNode.getId());
            tree.setName(treeNode.getName());
        });
        System.out.println(gettJsonString(treeList));
        System.out.println("=========");

        List<Tree<Long>> l2 = getAllChildren(treeList, 1L);
        System.out.println(gettJsonString(l2));
        System.out.println("-------");
        List<Tree<Long>> l3 = getAllChildren(treeList, 2L);
        System.out.println(gettJsonString(l3));
        System.out.println("-------");
        List<Tree<Long>> l4 = getAllChildren(treeList, 11L);
        System.out.println(gettJsonString(l4));
        System.out.println("-------");
        List<Tree<Long>> l5 = getAllChildren(treeList, 12L);
        System.out.println(gettJsonString(l5));
        System.out.println("-------");
    }

    private static <T> List<Tree<T>> getAllChildren(List<Tree<T>> treeList, T pId) {
          
   
        for (Tree<T> tree : treeList) {
          
   
            Tree<T> node = TreeUtil.getNode(tree, pId);
            if (node != null) {
          
   
                List<Tree<T>> children = new ArrayList<>();
                getAllChildren(node, children);
                return children;
            }
        }
        return new ArrayList<>();
    }

	/**
	 * 获取所有下节点
	 */
    private static <T> void getAllChildren(Tree<T> tree, List<Tree<T>> children) {
          
   
        List<Tree<T>> subChildren = tree.getChildren();
        if (!CollectionUtils.isEmpty(subChildren)) {
          
   
            children.addAll(subChildren);
            for (Tree<T> subChild : subChildren) {
          
   
                getAllChildren(subChild, children);
            }
        }
    }

    private static <T> String gettJsonString(T t) {
          
   
        return JSON.toJSONString(t, SerializerFeature.DisableCircularReferenceDetect);
    }

}
经验分享 程序员 微信小程序 职场和发展