java不需要递归列表转树形结构

有时候我们需要将列表结构的数据转成树形结构的数据
废话不多说直接上代码
基础类

@Data
public class TreeNode {
    private Long id;
    private Long parentId;
    private List<TreeNode> childrenList;
}

工具类

public class TreeNodeUtil {

    /**
     * 将列表转换成树。
     *
     * @param treeNodeList 列表数据
     * @param rootId       根节点id
     * @param rootType     根节点id类型 1-将rootId与id值相等的作为根节点,2-将rootId与parentId值相等的作为根节点
     * @param <T>
     * @return 返回树结构数据
     */
    public static <T extends TreeNode> List<T> listToTree(List<T> treeNodeList, Long rootId, int rootType) {
        // 储存根节点集合
        List<T> baseNodes = new ArrayList<>();
        // 节点id->对应的节点
        HashMap<Long, TreeNode> sonMap = new HashMap<>(baseNodes.size());
        // 节点id->节点对应的子节点
        HashMap<Long, List<TreeNode>> parentMap = new HashMap<>(baseNodes.size());

        for (T currentNode : treeNodeList) {
            Long currentId = currentNode.getId();
            Long parentId = currentNode.getParentId();

            // 把节点先放在Map中,为了以后获取当前节点的父节点
            sonMap.put(currentId, currentNode);

            if (parentMap.get(parentId) == null) {
                parentMap.put(parentId, new ArrayList<>());
            }
            //将子节点加入父节点
            parentMap.get(parentId).add(currentNode);

            // 判断当前节点是否有父节点
            if (sonMap.get(parentId) != null) {
                if (sonMap.get(parentId).getChildrenList() == null) {
                    sonMap.get(parentId).setChildrenList(new ArrayList<>());
                }
                // 将当前节点添加到父节点中
                sonMap.get(parentId).getChildrenList().add(currentNode);
            }

            // 判断当前节点是否有子节点
            if (!CollectionUtils.isEmpty(parentMap.get(currentId))) {
                //将所有的子节点加入到当前节点
                currentNode.setChildrenList(parentMap.get(currentId));
            }

            //根据节点id过滤根节点
            if (rootType == 1 && currentNode.getId().longValue() == rootId.longValue()) {
                baseNodes.add(currentNode);
            }

            //根据父节点id过滤根节点
            if (rootType == 2 && currentNode.getParentId().longValue() == rootId.longValue()) {
                baseNodes.add(currentNode);
            }
        }
        return baseNodes;
    }
}

测试类

public class TreeToListTest {
    @Test
    public void test() throws Exception {
        List<TreeNode> treeNodes=new ArrayList<>();
        treeNodes.add(createNode(1L,0L));
        treeNodes.add(createNode(2L,10L));
        treeNodes.add(createNode(3L,11L));
        treeNodes.add(createNode(4L,3L));
        treeNodes.add(createNode(5L,3L));
        treeNodes.add(createNode(6L,31L));
        treeNodes.add(createNode(7L,13L));
        treeNodes.add(createNode(8L,2L));
        treeNodes.add(createNode(9L,2L));
        treeNodes.add(createNode(10L,1L));
        treeNodes.add(createNode(11L,1L));
        treeNodes.add(createNode(12L,1L));
        treeNodes.add(createNode(13L,1L));
        List<TreeNode> treeNodeList1=TreeNodeUtil.listToTree(treeNodes,1L,1);
    }
}
原文地址:https://www.cnblogs.com/blueberry006/p/13322942.html