List 转树形结构

基本结构

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ListTreeImpl {

    //编号
    private Integer idx;

    //当前主键
    private String id;

    //上级主键
    private String pid;

    //子集集合
    private List<ListTreeImpl> children;
}

使用递归方式,获取根节点

    //获取树的根节点
    public static List<ListTreeImpl> getTreeRoot(ArrayList<ListTreeImpl> list) {
        ArrayList<ListTreeImpl> trees = new ArrayList<>();
        // 获取树的根节点
        for (ListTreeImpl tree : list) {
            String p = tree.getPid();
            boolean flag = false;
            for (ListTreeImpl tree1 : list) {
                // 循环判断当前是否在这个集合中有父节点
                if (tree1.getId() == null && p == null || tree1.getId().equals(p)) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                trees.add(tree);
            }
        }
        return trees;
    }

递归实现树

    //递归判断实现树结构
    public static void getTreesByRecursive(List<ListTreeImpl> trees, ArrayList<ListTreeImpl> list) {
        for (ListTreeImpl tree : trees) {
            String id = tree.getId();
            String pid = tree.getPid();
            List<ListTreeImpl> listTreeImpls = new ArrayList<>();
            for (ListTreeImpl listTreeImpl : list) {
                String pid1 = listTreeImpl.getPid();
                String id1 = listTreeImpl.getId();
                if (pid1 == null && pid == null || pid1 != null && pid1.equals(id)) {
                    if (!(id == null && id1 == null || id.equals(id1))) {
                        listTreeImpls.add(listTreeImpl);
                    }
                }
            }
            tree.setChildren(listTreeImpls);
            getTreesByRecursive(tree.getChildren(), list);
        }
    }

不使用递归方式1-获取每一个分层

    //获得分层的树(同一层的放一起)
    public static ArrayList<List<ListTreeImpl>> stratifying(ArrayList<ListTreeImpl> list) {
        ArrayList<List<ListTreeImpl>> arrayList = new ArrayList();
        if (list != null && list.size() > 0) {
            while (list.size() > 0) {
                List<ListTreeImpl> listTreeImpls = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    boolean flag = true;
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        ListTreeImpl externalTree = list.get(i);
                        ListTreeImpl innerTree = list.get(i1);
                        String externalTreePid = externalTree.getPid();
                        String innerTreePid = innerTree.getPid();
                        String innerTreeId = innerTree.getId();
                        if (!(externalTreePid == null && innerTreePid == null || externalTreePid != null && externalTreePid.equals(innerTreePid))) {
                            if (externalTreePid != null && externalTreePid.equals(innerTreeId)) {
                                flag = false;
                                break;
                            }
                        }
                    }
                    if (flag) {
                        listTreeImpls.add(list.get(i));
                    }
                }
                arrayList.add(listTreeImpls);
                list.removeAll(listTreeImpls);
            }
        }
        return arrayList;
    }

将分层结构弄成树

    //将分层的list的下级放入本级的children中
    public static List<ListTreeImpl> getTreeNo
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 在Java中,将List换为树形结构可以通过递归算法来实现。首先,我们需要定义一个树节点类Node,其包含一个value属性表示节点的值,以及一个List<Node>属性表示节点的子节点。 使用递归算法,我们可以遍历List,将每个元素作为一个节点添加到树中。对于每个节点,我们可以遍历List中的元素,如果元素的父节点值等于当前节点的值,则将该元素作为当前节点的子节点加入树中。 具体实现如下: ```java public class Node { private String value; private List<Node> children; public Node(String value) { this.value = value; this.children = new ArrayList<>(); } // getters and setters public static Node convertToTree(List<String> list) { Node root = new Node(""); convertToTree(list, root); return root; } private static void convertToTree(List<String> list, Node parent) { for (String value : list) { if (value.startsWith(parent.getValue())) { Node node = new Node(value); parent.getChildren().add(node); convertToTree(list, node); } } } } ``` 使用示例: ```java List<String> list = Arrays.asList("A", "A_B", "A_C", "A_C_D", "A_C_E", "B", "B_F"); Node root = Node.convertToTree(list); ``` 在上述示例中,我们将字符串列表list换成树形结构,其中每个字符串表示一个节点的值。在换后的树中,节点A下有节点B和节点C,节点C下有节点D和节点E,节点B下有节点F。 ### 回答2: 在Java中,将一个List换为树形结构可以通过递归的方式实现。下面是一个基本的示例代码: 首先,我们定义一个TreeNode类,表示树中的一个节点: ``` class TreeNode { private int id; private int parentId; private List<TreeNode> children; // 构造函数 public TreeNode(int id, int parentId) { this.id = id; this.parentId = parentId; this.children = new ArrayList<>(); } // Getters 和 Setters ... } ``` 接下来,我们可以编写一个递归的方法,用于将List换为树形结构: ``` public TreeNode buildTree(List<TreeNode> nodeList) { Map<Integer, TreeNode> nodeMap = new HashMap<>(); // 将所有节点以id为key存入map中 for (TreeNode node : nodeList) { nodeMap.put(node.getId(), node); } TreeNode root = null; // 遍历所有节点,通过parentId建立父子关系 for (TreeNode node : nodeList) { int parentId = node.getParentId(); if (parentId == 0) { root = node; // 根节点 } else { TreeNode parent = nodeMap.get(parentId); parent.getChildren().add(node); // 添加子节点 } } return root; // 返回根节点 } ``` 通过调用`buildTree`方法,我们可以将一个List换为树形结构: ``` List<TreeNode> nodeList = new ArrayList<>(); nodeList.add(new TreeNode(1, 0)); nodeList.add(new TreeNode(2, 1)); nodeList.add(new TreeNode(3, 1)); nodeList.add(new TreeNode(4, 2)); TreeNode root = buildTree(nodeList); ``` 这样,我们就成功将一个List换为树形结构了。在上面的例子中,根节点的id为1,其子节点有id为2和3,id为2的节点还有一个子节点id为4。 ### 回答3: Java中可以用List来存储树形结构的数据,然后通过递归的方式将List换为树形结构。 首先,可以定义一个节点类,其中包含节点的值以及节点的子节点列表。例如: ``` class TreeNode { private String value; // 节点的值 private List<TreeNode> children; // 节点的子节点列表 // 构造方法 public TreeNode(String value) { this.value = value; this.children = new ArrayList<>(); } // getter和setter方法 } ``` 然后,可以定义一个工具类来实现List换为树形结构的功能。在这个工具类中,可以使用递归的方式遍历List,使用每个节点的子节点列表来构建树形结构。例如: ``` class TreeUtils { public static TreeNode convertToTree(List<TreeNode> nodeList) { TreeNode root = new TreeNode("root"); // 创建根节点 for (TreeNode node : nodeList) { if (node.getValue().equals(root.getValue())) { continue; // 跳过根节点 } addChild(root, node); // 递归将子节点添加到树中 } return root; } private static void addChild(TreeNode parent, TreeNode child) { parent.getChildren().add(child); // 将子节点添加到父节点的子节点列表中 for (TreeNode node : child.getChildren()) { addChild(child, node); // 递归将子节点的子节点添加到树中 } } } ``` 最后,可以使用这个工具类将List换为树形结构。例如: ``` List<TreeNode> nodeList = new ArrayList<>(); // 添加节点到列表中 TreeNode tree = TreeUtils.convertToTree(nodeList); // 将列表换为树形结构 ``` 通过以上步骤,就可以将Java中的List换为树形结构,并且可以通过根节点来访问整个树的结构和数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值