效果图
2、主函数主函数
3、构建树 public static TreeNode buildTree(List<TreeNode> nodes) { Map<String, TreeNode> nodeMap = nodes.stream().collect(Collectors.toMap(TreeNode::getId, u -> u)); TreeNode root = null; for (TreeNode node : nodes) { if (node.parentId == null) { root = node; } else { TreeNode parent = nodeMap.get(node.parentId); parent.children.add(node); } } return root; }build Tree
love code
4、递归打印树public static void printTree(TreeNode node, int level) { if (node == null) return; for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println(node.itemCode); for (TreeNode child : node.children) { printTree(child, level + 1); } } public static void displayTree(List records){ List<TreeNode> treeNodes = new ArrayList<>(); // 生成 虚拟根节点 TreeNode rootNode = new TreeNode("root",null,"root"); treeNodes.add(rootNode); records.forEach(m ->{ String id = Convert.toStr(ReflectUtil.getFieldValue(m, "id")); String parentId = Convert.toStr(ReflectUtil.getFieldValue(m, "parentId")); String itemCode = Convert.toStr(ReflectUtil.getFieldValue(m, "itemCode")); TreeNode node = new TreeNode(id,parentId,itemCode); if(node.getParentId() == null) { node.setParentId("root"); } treeNodes.add(node); }); rootNode = buildTree(treeNodes); printTree(rootNode, 0); }display tree
源码工具类package com.sinopec.SICII.icm.utils;import cn.hutool.core.collection.CollUtil;import cn.hutool.core.convert.Convert;import cn.hutool.core.util.ReflectUtil;import lombok.Data;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public TreeToolUtils { @Data private static TreeNode { String id; String parentId; String itemCode; List<TreeNode> children; public TreeNode(String id, String parentId, String name) { this.id = id; this.parentId = parentId; this.itemCode = name; this.children = new ArrayList<>(); } } public static TreeNode buildTree(List<TreeNode> nodes) { Map<String, TreeNode> nodeMap = nodes.stream().collect(Collectors.toMap(TreeNode::getId, u -> u)); TreeNode root = null; for (TreeNode node : nodes) { if (node.parentId == null) { root = node; } else { TreeNode parent = nodeMap.get(node.parentId); parent.children.add(node); } } return root; } public static void printTree(TreeNode node, int level) { if (node == null) return; for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println(node.itemCode); for (TreeNode child : node.children) { printTree(child, level + 1); } } public static void displayTree(List records){ List<TreeNode> treeNodes = new ArrayList<>(); // 生成 虚拟根节点 TreeNode rootNode = new TreeNode("root",null,"root"); treeNodes.add(rootNode); records.forEach(m ->{ String id = Convert.toStr(ReflectUtil.getFieldValue(m, "id")); String parentId = Convert.toStr(ReflectUtil.getFieldValue(m, "parentId")); String itemCode = Convert.toStr(ReflectUtil.getFieldValue(m, "itemCode")); TreeNode node = new TreeNode(id,parentId,itemCode); if(node.getParentId() == null) { node.setParentId("root"); } treeNodes.add(node); }); rootNode = buildTree(treeNodes); printTree(rootNode, 0); } public static void main(String[] args) { List<TreeNode> nodes = CollUtil.newArrayList( new TreeNode("1", "root","x"), new TreeNode("2", "root","y"), new TreeNode("3", "2","y.1"), new TreeNode("4", "1","x.1"), new TreeNode("5", "3","y.1.1") ); displayTree(nodes); }}喜欢的点个关注 谢谢
树形目录