Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Java recursively gets TreeJson

Shulou Source: shulou.com Published: 2022-06-03 06:03:27 10月28日 Update

Union MyBatis, which is obtained by Map

The source code is as follows

Package com.zl.demo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Java recursively obtains TreeJson * @ author ZhangLi * @ date at 11:10:45 on June 18, 2019 * @ WeiXin zl4828 * @ original provision of foam gold foam, for reference only * / public class TreeJson {public static void main (String [] args) {TreeJson treeJson = new TreeJson () TreeJson.treeMap.add (new HashMap () {{put ("ID", "0"); put ("NAME", "root node"); put ("PID", "null");}}); treeJson.treeMap.add (new HashMap () {{put ("ID", "1"); put ("NAME", "Node 1") Put ("PID", "0");}}); treeJson.treeMap.add (new HashMap () {{put ("ID", "2"); put ("NAME", "Node 2"); put ("PID", "0");}}) TreeJson.treeMap.add (new HashMap () {{put ("ID", "3"); put ("NAME", "Node 3"); put ("PID", "0");}}); treeJson.treeMap.add (new HashMap () {{put ("ID", "11"); put ("NAME", "Node 11") Put ("PID", "1");}}); treeJson.treeMap.add (new HashMap () {{put ("ID", "12"); put ("NAME", "node 12"); put ("PID", "1");}}) TreeJson.treeMap.add (new HashMap () {{put ("ID", "13"); put ("NAME", "Node 13"); put ("PID", "1");}}); System.out.println (treeJson.recursiveTree ("0"));} private List treeMap = new ArrayList () / / Global variable / * * Recursive algorithm parses into a tree structure * @ param pid * / public Map recursiveTree (String pid) {Map node = getNodeById (pid); List childrenList = new ArrayList (); if (null==node) {return node;} List childTreeNodes = getChildTreeById (pid) For (Map child: childTreeNodes) {Map n = recursiveTree (String.valueOf (child.get ("ID"); childrenList.add (n);} node.put ("children", childrenList); return node } / * query node objects according to pid * / public Map getNodeById (String pid) {for (Map node: treeMap) {if (null! = pid) {if (pid.equals (node.get ("ID") {return node;} return null } / * all nodes are obtained according to the parent node pid * / public List getChildTreeById (String pid) {List list = new ArrayList () If (null! = treeMap) {for (Map node: treeMap) {if (null! = pid) {if (pid.equals (node.get ("PID") {list.add (node);} return list }}

Application scenario

For example, the Echarts tree graph

Note that using JSON transformations

{name: 'root node', pid: null, id: 0, children: [{name: 'node 1 node, pid: 0, id: 1, children: [{name:' node 11 node, pid: 1, id: 11, children: []} {name: 'node 12, pid: 1, id: 12, children: []}, {name:' node 13, pid: 1, id: 13, children: []}}, {name: 'node 2' Pid: 0, id: 2, children: []}, {name: 'node 3percent, pid: 0, id: 3, children: []}}

Above, copy and run locally, any application.

Tags: Node recursion tree application for reference only global variable graphics scene comment object copy source code algorithm structure query federation run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Shulou Information macOS NVidia