java生成树结构
作者:互联网
转自:https://www.cnblogs.com/lucky-pin/p/10740037.html
JAVA递归生成树形菜单
递归生成一个如图的菜单,编写两个类数据模型Menu、和创建树形的MenuTree。通过以下过程实现:
1.首先从菜单数据中获取所有根节点。
2.为根节点建立次级子树并拼接上。
3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”。
首先,编写数据模型Menu。每条菜单有自己的id、父节点parentId、菜单名称text、菜单还拥有次级菜单children。
1 import java.util.List; 2 3 public class Menu { 4 private String id; 5 private String parentId; 6 private String text; 7 private String url; 8 private String yxbz; 9 private List<Menu> children; 10 public Menu(String id,String parentId,String text,String url,String yxbz) { 11 this.id=id; 12 this.parentId=parentId; 13 this.text=text; 14 this.url=url; 15 this.yxbz=yxbz; 16 } 17 /*省略get\set*/ 18 }
创建树形结构的类MenuTree。方法getRootNode获取所有根节点,方法builTree将根节点汇总创建树形结构,buildChilTree为节点建立次级树并拼接上当前树,递归调用buildChilTree不断为当前树开枝散叶直至找不到新的子树。完成递归,获取树形结构。
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class MenuTree { 5 private List<Menu> menuList = new ArrayList<Menu>(); 6 public MenuTree(List<Menu> menuList) { 7 this.menuList=menuList; 8 } 9 10 //建立树形结构 11 public List<Menu> builTree(){ 12 List<Menu> treeMenus =new ArrayList<Menu>(); 13 for(Menu menuNode : getRootNode()) { 14 menuNode=buildChilTree(menuNode); 15 treeMenus.add(menuNode); 16 } 17 return treeMenus; 18 } 19 20 //递归,建立子树形结构 21 private Menu buildChilTree(Menu pNode){ 22 List<Menu> chilMenus =new ArrayList<Menu>(); 23 for(Menu menuNode : menuList) { 24 if(menuNode.getParentId().equals(pNode.getId())) { 25 chilMenus.add(buildChilTree(menuNode)); 26 } 27 } 28 pNode.setChildren(chilMenus); 29 return pNode; 30 } 31 32 //获取根节点 33 private List<Menu> getRootNode() { 34 List<Menu> rootMenuLists =new ArrayList<Menu>(); 35 for(Menu menuNode : menuList) { 36 if(menuNode.getParentId().equals("0")) { 37 rootMenuLists.add(menuNode); 38 } 39 } 40 return rootMenuLists; 41 } 42 }
最后,插入一些数据试试效果。得到的json就可以生成图一菜单了。
1 import java.util.ArrayList; 2 import java.util.List; 3 import com.alibaba.fastjson.JSON; 4 5 public class Hello { 6 public static void main(String []args) { 7 List<Menu> menuList= new ArrayList<Menu>(); 8 /*插入一些数据*/ 9 menuList.add(new Menu("GN001D000","0","系统管理","/admin","Y")); 10 menuList.add(new Menu("GN001D100","GN001D000","权限管理","/admin","Y")); 11 menuList.add(new Menu("GN001D110","GN001D100","密码修改","/admin","Y")); 12 menuList.add(new Menu("GN001D120","GN001D100","新加用户","/admin","Y")); 13 menuList.add(new Menu("GN001D200","GN001D000","系统监控","/admin","Y")); 14 menuList.add(new Menu("GN001D210","GN001D200","在线用户","/admin","Y")); 15 menuList.add(new Menu("GN002D000","0","订阅区","/admin","Y")); 16 menuList.add(new Menu("GN003D000","0","未知领域","/admin","Y")); 17 /*让我们创建树*/ 18 MenuTree menuTree =new MenuTree(menuList); 19 menuList=menuTree.builTree(); 20 /*转为json看看效果*/ 21 String jsonOutput= JSON.toJSONString(menuList); 22 System.out.println(jsonOutput); 23 } 24 }
@luqp 本文地址:https://www.cnblogs.com/lucky-pin/p/10740037.html
#4楼 2021-11-03 18:32 kam1996
我用kotlin简单实现了一个构建tree的小工具,只需要两个类,一个tree构建类,一个node接口,
代码如下:
tree构建类:
`
class TreeBuilder<T : TreeNode<T>>(private var nodes: MutableList<T>) {
/**
* 构建树
*/
fun build(): List<T> {
return this.obtainRootNode().map { parent ->
this.buildChildTree(parent)
}
}
/**
* 构建子树
*/
private fun buildChildTree(parentNode: T): T {
val childList = nodes.filter { it.obtainParentId() == parentNode.obtainId() }
.map { buildChildTree(it) }
parentNode.fillChildList(childList)
return parentNode
}
/**
* 获取根节点
*/
private fun obtainRootNode(): List<T> {
return nodes.filter { it.obtainParentId() == 0L }
}
}
`
treeNode接口:
`
interface TreeNode<T> {
/**
* 获取ID
*/
fun obtainId(): Long
/**
* 获取父ID
*/
fun obtainParentId(): Long
/**
* 获取下一子节点列表
*/
fun obtainChildList(): List<T>
/**
* 填充子节点列表
*/
fun fillChildList(childNodes: List<T>)
}
`
实际使用测试类:
`
fun main() {
val list = mutableListOf<Resource>()
list.add(Resource(1, 0,"zhangsan", null))
list.add(Resource(2, 0,"lisi", null))
list.add(Resource(3, 1,"wangwu", null))
list.add(Resource(4, 1,"zhaoliu", null))
list.add(Resource(5, 1,"liqing", null))
list.add(Resource(6, 2,"yanlai", null))
list.add(Resource(7, 2,"kaisen", null))
list.add(Resource(8, 3,"niubi", null))
list.add(Resource(9, 3,"haha", null))
val tree = TreeBuilder(list).build()
println(JSON.toJSONString(tree))
}
class Resource(var id: Long?, var parentId: Long?, var name: String?, var childList: List<Resource>?) :
TreeNode<Resource> {
override fun obtainId(): Long {
return id!!
}
override fun obtainParentId(): Long {
return parentId!!
}
override fun obtainChildList(): List<Resource> {
return childList!!
}
override fun fillChildList(childNodes: List<Resource>) {
this.childList = childNodes
}
}
返回的结果:
[{
"childList": [{
"childList": [{
"childList": [],
"id": 8,
"name": "niubi",
"parentId": 3
}, {
"childList": [],
"id": 9,
"name": "haha",
"parentId": 3
}],
"id": 3,
"name": "wangwu",
"parentId": 1
}, {
"childList": [],
"id": 4,
"name": "zhaoliu",
"parentId": 1
}, {
"childList": [],
"id": 5,
"name": "liqing",
"parentId": 1
}],
"id": 1,
"name": "zhangsan",
"parentId": 0
}, {
"childList": [{
"childList": [],
"id": 6,
"name": "yanlai",
"parentId": 2
}, {
"childList": [],
"id": 7,
"name": "kaisen",
"parentId": 2
}],
"id": 2,
"name": "lisi",
"parentId": 0
}]
`
标签:java,树结构,Menu,List,生成,add,parentId,id,menuList 来源: https://www.cnblogs.com/sensenh/p/15797660.html