举例部门级别
//节点对象类
package com.test;
import java.util.List;
/**
* ClassName: TestMode
* @Description: 测试对象
*/
public class TestMode {
/** 节点id */
private String id;
/** 节点名称 */
private String name;
/** 父节点id */
private String parentId;
/** 节点下的子节点 */
private List children;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
@Override
public String toString() {
return "{"+"id=" + id + ", name=" + name + ", parentId=" + parentId + ", children=" + children+"}";
}
}
测试类
package com.test;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 转化List为树形结构
*/
public class ListToTreeUtil {
public static void main(String[] args) {
List list = new ArrayList();
TestMode testMode1 = new TestMode();
testMode1.setId("-1");
testMode1.setName("父节点");
testMode1.setParentId("0");//其实-1节点没有父节点,只是为了方便
TestMode testMode2 = new TestMode();
testMode2.setId("1");
testMode2.setName("第一级节点1");
testMode2.setParentId("-1");
TestMode testMode3 = new TestMode();
testMode3.setId("2");
testMode3.setName("第一级节点2");
testMode3.setParentId("-1");
TestMode testMode4 = new TestMode();
testMode4.setId("11");
testMode4.setName("第二级节点11");
testMode4.setParentId("1");
TestMode testMode5 = new TestMode();
testMode5.setId("111");
testMode5.setName("第三级节点111");
testMode5.setParentId("11");
TestMode testMode6 = new TestMode();
testMode6.setId("21");
testMode6.setName("第二级级节点21");
testMode6.setParentId("2");
list.add(testMode1);
list.add(testMode2);
list.add(testMode3);
list.add(testMode4);
list.add(testMode5);
list.add(testMode6);
System.out.println("处理前的数据格式:"+ list.toString());
list = getDeptTree(list,"0");
System.out.println("处理后的数据格式:"+ list.toString());
}
public static List getDeptTree(List list,String pid){
List result = new ArrayList();
List temp = new ArrayList();
for(TestMode entity : list){
if(entity.getParentId().equals(pid)){
TestMode deptScopeMode = new TestMode();
deptScopeMode.setId(entity.getId());
deptScopeMode.setName(entity.getName());
deptScopeMode.setParentId(entity.getParentId());
temp = getDeptTree(list,entity.getId());
if(temp.size() > 0){
deptScopeMode.setChildren(temp);
}
result.add(deptScopeMode);
}
}
return result;
}
}
处理签的数据格式:
[{
id = -1,
name = 父节点,
parentId = 0,
children = null
}, {
id = 1,
name = 第一级节点1,
parentId = -1,
children = null
}, {
id = 2,
name = 第一级节点2,
parentId = -1,
children = null
}, {
id = 11,
name = 第二级节点11,
parentId = 1,
children = null
}, {
id = 111,
name = 第三级节点111,
parentId = 11,
children = null
}, {
id = 21,
name = 第二级级节点21,
parentId = 2,
children = null
}]
处理后的数据格式:
[{
id = -1,
name = 父节点,
parentId = 0,
children = [{
id = 1,
name = 第一级节点1,
parentId = -1,
children = [{
id = 11,
name = 第二级节点11,
parentId = 1,
children = [{
id = 111,
name = 第三级节点111,
parentId = 11,
children = null
}]
}]
}, {
id = 2,
name = 第一级节点2,
parentId = -1,
children = [{
id = 21,
name = 第二级级节点21,
parentId = 2,
children = null
}]
}]
}]