java转树结构,java-树形结构的list,转化为树形结构

举例部门级别

//节点对象类

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 Listchildren;

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 ListgetChildren() {

return children;

}

public void setChildren(Listchildren) {

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) {

Listlist = 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 ListgetDeptTree(Listlist,String pid){

Listresult = new ArrayList();

Listtemp = 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

}]

}]

}]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值