Simple fast generation tree and traversing tree tools
1.use maven
<dependency>
<groupId>com.github.xchendeveloper</groupId>
<artifactId>JTree</artifactId>
<version>1.0</version>
</dependency>
2.download code by command :
git clone https://github.com/xchendeveloper/JTree.git
Specify the name of id and parentId of dataItem
Jtree jtree = JTree.init("mytree","xxxid","xxxpid");
Using "id" and "pid" as data items by default
Jtree jtree = JTree.init("mytree");
Node tree = jtree.generalTree(dataList);
// For details, please see test case
we can also do it when we init JTree
Node tree = JTree.init("mytree").generalTree(dataList);
Map<String,Object> map = tree.toMap();
if your dateItem is not javaBean,toMap() function will failed,you should implement your own NodeData2MapGenerator,eg:
public class Bean2MapGenerator implements NodeData2MapGenerator {
public Map generate(Object obj) {
return BeanUtil.beanToMap(obj);
}
}
and use your generator like this:
tree.toMap(new Bean2MapGenerator);
Node node = JTree.init("mytree").generalTree(dataList);
List<Node> nodeList = new ArrayList<Node>();
JTree.get("mytree").treeToNodeList(node,nodeList);
Node node = JTree.init("mytree").generalTree(dataList);
Node node = JTree.get("mytree").getNodeById(node,3);
Node node = JTree.init("mytree").generalTree(dataList);
Node node1 = JTree.get("mytree").getNodeById(node,3);
// getSubNodeList(Node node, boolean recursion, boolean includeSelf)
List<Node> nodeList = JTree.get("mytree").getSubNodeList(node1,true,true);
Node node = JTree.init("mytree").generalTree(dataList);
List<Node> nodeList = JTree.get("mytree").getTreeSubNodeListById(node,3,true,true);
Node node = JTree.init("mytree").generalTree(dataList);
Node node1 = JTree.get("mytree").getNodeById(node,3);
List<Object> idList = JTree.get("mytree").getSubNodeIdList(node1,true,true);
Node node = JTree.init("mytree").generalTree(dataList);
List<Object> nodeList = JTree.get("mytree").getTreeSubNodeIdListById(node,3,true,true);