树形结构工具类

public class TreeNode<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 主键
     */
    private Long id;
    /**
     * 上级ID
     */
    private Long pid;
    /**
     * 子节点列表
     */
    private List<T> children = new ArrayList<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public List<T> getChildren() {
        return children;
    }

    public void setChildren(List<T> children) {
        this.children = children;
    }
}

对树操作工具:

public class TreeUtils {

    /**
     * 根据pid,构建树节点
     */
    public static <T extends TreeNode> List<T> build(List<T> treeNodes, Long pid) {
        List<T> treeList = new ArrayList<>();
        for(T treeNode : treeNodes) {
            if (pid.equals(treeNode.getPid())) {
                treeList.add(findChildren(treeNodes, treeNode));
            }
        }

        return treeList;
    }

    /**
     * 查找子节点
     */
    private static <T extends TreeNode> T findChildren(List<T> treeNodes, T rootNode) {
        for(T treeNode : treeNodes) {
            if(rootNode.getId().equals(treeNode.getPid())) {
                rootNode.getChildren().add(findChildren(treeNodes, treeNode));
            }
        }
        return rootNode;
    }

    /**
     * 构建树节点
     */
    public static <T extends TreeNode> List<T> build(List<T> treeNodes) {
        List<T> result = new ArrayList<>();
        //list转map
        Map<Long, T> nodeMap = new LinkedHashMap<>(treeNodes.size());
        for(T treeNode : treeNodes){
            nodeMap.put(treeNode.getId(), treeNode);
        }
        for(T node : nodeMap.values()) {
            T parent = nodeMap.get(node.getPid());
            if(parent != null && !(node.getId().equals(parent.getId()))){
                parent.getChildren().add(node);
                continue;
            }

            result.add(node);
        }
        return result;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Java 工具类,可以将树形结构数据输出为字符串: ```java import java.util.List; public class TreePrinter { private static final String LINE_PREFIX = " "; private static final String BRANCH_PREFIX = "│ "; private static final String LAST_BRANCH_PREFIX = "└── "; public static <T extends TreeNode> String printTree(T root) { StringBuilder sb = new StringBuilder(); printNode(sb, "", root, true); return sb.toString(); } private static <T extends TreeNode> void printNode(StringBuilder sb, String prefix, T node, boolean isLast) { sb.append(prefix); if (isLast) { sb.append(LAST_BRANCH_PREFIX); prefix += LINE_PREFIX; } else { sb.append(BRANCH_PREFIX); prefix += BRANCH_PREFIX; } sb.append(node.getName()); sb.append("\n"); List<T> children = node.getChildren(); if (children != null) { int size = children.size(); for (int i = 0; i < size; i++) { boolean last = i == size - 1; printNode(sb, prefix, children.get(i), last); } } } public interface TreeNode { String getName(); List<? extends TreeNode> getChildren(); } } ``` 使用方法: 1. 实现 `TreeNode` 接口,并实现其中的两个方法 `getName()` 和 `getChildren()`,分别返回节点名称和子节点列表。 2. 调用 `TreePrinter.printTree(root)` 方法,其中 `root` 为树的根节点。 以下是一个简单的例子: ```java import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { TreeNode root = new Node("root"); Node node1 = new Node("node1"); Node node2 = new Node("node2"); Node node3 = new Node("node3"); Node node4 = new Node("node4"); Node node5 = new Node("node5"); Node node6 = new Node("node6"); root.addChild(node1); root.addChild(node2); root.addChild(node3); node2.addChild(node4); node2.addChild(node5); node3.addChild(node6); String tree = TreePrinter.printTree(root); System.out.println(tree); } static class Node implements TreePrinter.TreeNode { private String name; private List<Node> children; public Node(String name) { this.name = name; this.children = new ArrayList<>(); } public void addChild(Node child) { children.add(child); } @Override public String getName() { return name; } @Override public List<Node> getChildren() { return children; } } } ``` 输出结果为: ``` └── root ├── node1 ├── node2 │ ├── node4 │ └── node5 └── node3 └── node6 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值