In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.