树形工具类

@Data
class Node extends BaseNode<String, Node> {
        private String c ;
}
import java.util.List;

class BaseNode<K, T> {
    K id;
    K pid;
    List<T> children;

    public K getId() {
        return id;
    }

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

    public K getPid() {
        return pid;
    }

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

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

    public void setChildren(List<T> children) {
        this.children = children;
    }
}
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

public class TreeToolUtils<T extends BaseNode> {

    private List<T> rootList; //根节点对象存放到这里

    private List<T> bodyList; //其他节点存放到这里,可以包含根节点

    public TreeToolUtils(List<T> rootList, List<T> bodyList) {
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    public List<T> getTree(){   //调用的方法入口
        if(bodyList != null && !bodyList.isEmpty()){
        //声明一个map,用来过滤已操作过的数据
            Map<Object,Object> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree,map));
            return rootList;
        }
        return rootList;
    }

    public void getChild(T beanTree,Map<Object,Object> map){
        List<T> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getId()))
                .filter(c ->c.getPid().equals(beanTree.getId()))
                .forEach(c ->{
                    map.put(c.getId(),c.getPid());
                    getChild(c,map);
                    childList.add(c);
                });
        beanTree.setChildren(childList);
    }

    public static void main(String[] args) {
        Node node = new Node();
        node.setId("1");
        node.setPid("1");
        node.setC("fasdA");
        Node b = new Node();
        b.setId("2");
        b.setPid("1");
        b.setC("fasdB");
        Node c = new Node();
        c.setId("3");
        c.setPid("1");
        c.setC("fasdC");
        List<Node> root = Lists.newArrayList(node);
        List<Node> body =  Lists.newArrayList(b,c);
        TreeToolUtils<Node> treeToolUtils = new TreeToolUtils<>(root,body);
        List<Node> tree = treeToolUtils.getTree();
        System.err.println(tree);
        System.err.println(JSONObject.toJSONString(tree));
    }
}

测试结果:

[{"c":"fasdA","children":[{"c":"fasdB","children":[],"id":"2","pid":"1"},{"c":"fasdC","children":[],"id":"3","pid":"1"}],"id":"1","pid":"1"}]

  • 1
    点赞
  • 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
发出的红包

打赏作者

没事搞点事做serendipity

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值