Java 构建树型结构
常见需求
- 菜单导航
- 层叠列表
- 权限树
- …
构建思路
编写两个POJO: Item、和 创建树型的ItemTree。通过以下过程实现:
- 根据标示,获取所有根节点.
- 为根节点建立次级节点并拼接上.
- 递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”
代码
1.ItemMenu 存储每个节点相关的属性
import java.util.List; /** * @author : Jay * @description : 节点对象 * @date : 2021-07-14 09:24 **/ public class Item { /** * ID */ private String id; /** * 父节点ID */ private String parentId; /** * 节点名称 */ private String text; /** * 子节点集合 */ private List<Item> children; public Item(String id, String parentId, String text) { this.id=id; this.parentId=parentId; this.text=text; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public String getText() { return text; } public void setText(String text) { this.text = text; } public List<Item> getChildren() { return children; } public void setChildren(List<Item> children) { this.children = children; } @Override public String toString() { return "Menu{" + "id='" + id + '\'' + ", parentId='" + parentId + '\'' + ", text='" + text + '\'' + ", children=" + children + '}'; } }
2.树, 用于构建树使用的POJO
import java.util.ArrayList; import java.util.List; /** * @author : Jay * @description : 树 * @date : 2021-07-14 09:27 **/ public class ItemTree { /** * 所有菜单集合,构建成树形结构前 */ private List<Item> itemList = new ArrayList<Item>(); public ItemTree(List<Item> itemList) { this.itemList = itemList; } /** * 构建树形结构 */ public List<Item> buildTree(){ List<Item> treeItems = new ArrayList<Item>(); //遍历根节点 for(Item itemNode : getRootNode()) { //递归处理每一个根节点 itemNode = buildChildTree(itemNode); treeItems.add(itemNode); } return treeItems; } /** * 递归,建立子树形结构 * @param parentNode 父节点 * @return 构建后的父节点 */ private Item buildChildTree(Item parentNode){ List<Item> childItems = new ArrayList<Item>(); for(Item itemNode : itemList) { // 当前节点的父节点ID等于传入的父节点,则放入子节点集合中 if(itemNode.getParentId().equals(parentNode.getId())) { childItems.add(buildChildTree(itemNode)); } } parentNode.setChildren(childItems); return parentNode; } /** * 获取根节点(父节点ID为0) */ private List<Item> getRootNode() { List<Item> rootItemLists = new ArrayList<Item>(); for(Item itemNode : itemList) { //当节点的父节点为0,则为根节点 if(itemNode.getParentId().equals("0")) { rootItemLists.add(itemNode); } } return rootItemLists; } @Override public String toString() { return "ItemTree{" + "itemList=" + itemList + '}'; } }
3.测试
import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.List; /** * @author : Jay * @description : 树形结构测试类 * @date : 2021-07-14 09:58 **/ public class TreeDemo { public static void main(String[] args) { List<Item> itemList = new ArrayList<Item>(); //模拟数据 itemList.add(new Item("GN001D000","0","系统管理")); itemList.add(new Item("GN001D100","GN001D000","权限管理")); itemList.add(new Item("GN001D110","GN001D100","密码修改")); itemList.add(new Item("GN001D120","GN001D100","新加用户")); itemList.add(new Item("GN001D200","GN001D000","系统监控")); itemList.add(new Item("GN001D210","GN001D200","在线用户")); itemList.add(new Item("GN002D000","0","订阅区")); itemList.add(new Item("GN003D000","0","未知领域")); //创建树 ItemTree itemTree = new ItemTree(itemList); //构建树 itemList = itemTree.buildTree(); String jsonOutput= JSONObject.toJSONString(itemList); System.out.println(jsonOutput); } }
4.测试结果
[ { "children":[ { "children":[ Object{...}, Object{...} ], "id":"GN001D100", "parentId":"GN001D000", "text":"权限管理", "url":"/admin", "yxbz":"Y" }, { "children":[ { "children":[ ], "id":"GN001D210", "parentId":"GN001D200", "text":"在线用户", "url":"/admin", "yxbz":"Y" } ], "id":"GN001D200", "parentId":"GN001D000", "text":"系统监控", "url":"/admin", "yxbz":"Y" } ], "id":"GN001D000", "parentId":"0", "text":"系统管理", "url":"/admin", "yxbz":"Y" }, { "children":[ ], "id":"GN002D000", "parentId":"0", "text":"订阅区", "url":"/admin", "yxbz":"Y" }, { "children":[ ], "id":"GN003D000", "parentId":"0", "text":"未知领域", "url":"/admin", "yxbz":"Y" } ]
5.完成
关注公众号:熊猫Jay字节之旅,了解更多 AI 技巧 ~