1. 节点 model 类
package com.huiyou.yzf.tree;
import java.util.List;
public class TreeNode {
private String id; //编号(不一定是主键)
private String parentId; //父级编号
private String text; //显示名称
private String state;//combotree 设置为closed 则默认此节点不展开
private List<TreeNode> children;
public TreeNode(String id, String text, String parentId) {
this.id = id;
this.parentId = parentId;
this.text = text;
}
public TreeNode(String id, String text, TreeNode parent) {
this.id = id;
this.parentId = parent.getId();
this.text = text;
}
public TreeNode(String id, String text, String parentId,String state) {
this.id = id;
this.parentId = parentId;
this.text = text;
this.state = state;
}
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 String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "TreeNode{" +
"id='" + id + '\'' +
", parentId='" + parentId + '\'' +
", text='" + text + '\'' +
", state='" + state + '\''+
", children=" + children +
'}';
}
}
2.树形 List 构造器(两种方式:1 循环 2 递归)---配有测试 main 方法
package com.huiyou.yzf.tree;
import java.util.ArrayList;
import java.util.List;
public class TreeBuider {
/**
* 两层循环实现建树
* @param treeNodes 传入的树节点列表
* @return
*/
public static List<TreeNode> bulid(List<TreeNode> treeNodes) {
List<TreeNode> trees = new ArrayList<TreeNode>();
for (TreeNode treeNode : treeNodes) {
if ("0".equals(treeNode.getParentId())) {
trees.add(treeNode);
}
for (TreeNode it : treeNodes) {
if (it.getParentId().equals(treeNode.getId())) {
if (treeNode.getChildren() == null) {
treeNode.setChildren(new ArrayList<TreeNode>());
}
treeNode.getChildren().add(it);
}
}
}
return trees;
}
/**
* 使用递归方法建树
* @param treeNodes
* @return
*/
public static List<TreeNode> buildByRecursive(List<TreeNode> treeNodes) {
List<TreeNode> trees = new ArrayList<TreeNode>();
for (TreeNode treeNode : treeNodes) {
if ("0".equals(treeNode.getParentId())) {
trees.add(findChildren(treeNode,treeNodes));
}
}
return trees;
}
/**
* 递归查找子节点
* @param treeNodes
* @return
*/
public static TreeNode findChildren(TreeNode treeNode,List<TreeNode> treeNodes) {
for (TreeNode it : treeNodes) {
if(treeNode.getId().equals(it.getParentId())) {
if (treeNode.getChildren() == null) {
treeNode.setChildren(new ArrayList<TreeNode>());
}
treeNode.getChildren().add(findChildren(it,treeNodes));
}
}
return treeNode;
}
public static void main(String[] args) {
TreeNode treeNode1 = new TreeNode("1","广州","0");
TreeNode treeNode2 = new TreeNode("2","深圳","0");
TreeNode treeNode3 = new TreeNode("3","天河区",treeNode1);
TreeNode treeNode4 = new TreeNode("4","越秀区",treeNode1);
TreeNode treeNode5 = new TreeNode("5","黄埔区",treeNode1);
TreeNode treeNode6 = new TreeNode("6","石牌",treeNode3);
TreeNode treeNode7 = new TreeNode("7","百脑汇",treeNode6);
TreeNode treeNode8 = new TreeNode("8","南山区",treeNode2);
TreeNode treeNode9 = new TreeNode("9","宝安区",treeNode2);
TreeNode treeNode10 = new TreeNode("10","科技园",treeNode8);
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(treeNode1);
list.add(treeNode2);
list.add(treeNode3);
list.add(treeNode4);
list.add(treeNode5);
list.add(treeNode6);
list.add(treeNode7);
list.add(treeNode8);
list.add(treeNode9);
list.add(treeNode10);
List<TreeNode> trees = bulid(list);
System.out.println(trees);
List<TreeNode> trees_ = buildByRecursive(list);
System.out.println(trees_);
}
}
节点 model 类中属性 可根据实际情况进行改变,当 list 存在多级关系时,常会在 Java 组装成树形 Json 数据返回,将数据与节点 model 对应,用上述构造器 中任一方法都可实现 多级 list 数据组装,然后进行后续处理。
以前借鉴某位博主的,地址已找不到,最近又用到 ,在此记录一下,主要供日后使用。
————————————————
版权声明:本文为 CSDN 博主「牛肉炒刀削」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ZaberyJava/article/details/81914857
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于