Java将List<T>集合组装成树(Tree)树结构组装
把列表转换为树结构
/**
* 把列表转换为树结构
*
* @param originalList 原始list数据
* @param keyName 作为唯一标示的字段名称
* @return 组装后的集合
*/
public static <T> List<T> getTree(List<T> originalList, String keyName) throws Exception {
String parentFieldName = "parentId";
String childrenFieldName = "children"; // 获取根节点,即找出父节点为空的对象
List<T> topList = new ArrayList<>();
for (int i = 0; i < originalList.size(); i++) {
T t = originalList.get(i);
String parentId = BeanUtils.getProperty(t, parentFieldName);
if (StringUtils.isBlank(parentId)) {
topList.add(t);
}
} // 将根节点从原始list移除,减少下次处理数据
originalList.removeAll(topList); // 递归封装树
fillTree(topList, originalList, keyName, parentFieldName, childrenFieldName); return topList;
}
封装树
/**
* 封装树
*
* @param parentList 要封装为树的父对象集合
* @param originalList 原始list数据
* @param keyName 作为唯一标示的字段名称
* @param parentFieldName 模型中作为parent字段名称
* @param childrenFieldName 模型中作为children的字段名称
*/
public static <T> void fillTree(List<T> parentList, List<T> originalList, String keyName, String parentFieldName, String childrenFieldName) throws Exception {
for (int i = 0; i < parentList.size(); i++) {
List<T> children = fillChildren(parentList.get(i), originalList, keyName, parentFieldName, childrenFieldName);
if (children.isEmpty()) {
continue;
}
originalList.removeAll(children);
fillTree(children, originalList, keyName, parentFieldName, childrenFieldName);
}
}
封装子对象
/**
* 封装子对象
*
* @param parent 父对象
* @param originalList 待处理对象集合
* @param keyName 作为唯一标示的字段名称
* @param parentFieldName 模型中作为parent字段名称
* @param childrenFieldName 模型中作为children的字段名称
*/
public static <T> List<T> fillChildren(T parent, List<T> originalList, String keyName, String parentFieldName, String childrenFieldName) throws Exception {
List<T> childList = new ArrayList<>();
String parentId = BeanUtils.getProperty(parent, keyName);
for (int i = 0; i < originalList.size(); i++) {
T t = originalList.get(i);
String childParentId = BeanUtils.getProperty(t, parentFieldName);
if (parentId.equals(childParentId)) {
childList.add(t);
}
}
if (!childList.isEmpty()) {
FieldUtils.writeDeclaredField(parent, childrenFieldName, childList, true);
}
return childList;
}
测试实体类
/**
* 测试实体类
*/
public class Catalog { /**
* 唯一编号 uuid
*/
private String id; /**
* 名称
*/
private String name; /**
* 父节点id
*/
private String parentId; /**
* 子节点(数据库中不存在该字段,仅用于传输数据使用)
*/
private List<?> children; // 省略 get set
}
测试
/**
* 测试 一共有六个Catalog
* 其中:name1下面有三个子节点:name2、name3、name4
* name2下面有两个子节点:name5、name6
*/
@Test
public void ListToMap() throws Exception {
List<Catalog> list = new ArrayList<>(); Catalog catalog = new Catalog();
String flowId = randomUUID();
catalog.setFlowId(flowId);
catalog.setName("name1");
list.add(catalog); catalog = new Catalog();
String flowId2 = randomUUID();
catalog.setFlowId(flowId2);
catalog.setName("name2");
catalog.setParentId(flowId);
list.add(catalog); catalog = new Catalog();
String flowId3 = randomUUID();
catalog.setFlowId(flowId3);
catalog.setName("name3");
catalog.setParentId(flowId);
list.add(catalog); catalog = new Catalog();
String flowId4 = randomUUID();
catalog.setFlowId(flowId4);
catalog.setName("name4");
catalog.setParentId(flowId);
list.add(catalog); catalog = new Catalog();
String flowId5 = randomUUID();
catalog.setFlowId(flowId5);
catalog.setName("name5");
catalog.setParentId(flowId2);
list.add(catalog); catalog = new Catalog();
String flowId6 = randomUUID();
catalog.setFlowId(flowId6);
catalog.setName("name6");
catalog.setParentId(flowId2);
list.add(catalog); List<Catalog> tree = getTree(list, "flowId");
System.out.println(JSON.toJSONString(tree)); /* 装换后输出json样式
[
{
"id" : "ee55dafee60d44a3a143bd3623f29aa9",
"name" : "name1",
"children" : [
{
"id" : "2e049bdb67624054ad989b511f1b7674",
"parentId" : "ee55dafee60d44a3a143bd3623f29aa9",
"name" : "name2",
"children" : [
{
"id" : "9292a15c7a5e4983ac03d10c5fe12a14",
"name" : "name5",
"parentId" : "2e049bdb67624054ad989b511f1b7674"
},
{
"id" : "6cd9608726a1438682f239b6017680ca",
"name" : "name6",
"parentId" : "2e049bdb67624054ad989b511f1b7674"
}
]
},
{
"id" : "98e90c9e23e445f980c4116bd4c83233",
"name" : "name3",
"parentId" : "ee55dafee60d44a3a143bd3623f29aa9"
},
{
"id" : "5e67e73c6aef4eb19b63b4789dcc3486",
"name" : "name4",
"parentId" : "ee55dafee60d44a3a143bd3623f29aa9"
}
]
}
]
*/
} protected String randomUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
Java将List<T>集合组装成树(Tree)树结构组装的更多相关文章
- Java多线程系列--“JUC集合”05之 ConcurrentSkipListMap
概要 本章对Java.util.concurrent包中的ConcurrentSkipListMap类进行详细的介绍.内容包括:ConcurrentSkipListMap介绍ConcurrentSki ...
- Java-杂项:Java数组Array和集合List、Set、Map
ylbtech-Java-杂项:Java数组Array和集合List.Set.Map 1.返回顶部 1. 之前一直分不清楚java中的array,list.同时对set,map,list的用法彻底迷糊 ...
- Java中如何克隆集合——ArrayList和HashSet深拷贝
编程人员经常误用各个集合类提供的拷贝构造函数作为克隆List,Set,ArrayList,HashSet或者其他集合实现的方法.需要记住的是,Java集合的拷贝构造函数只提供浅拷贝而不是深拷贝,这意味 ...
- Java多线程系列--“JUC集合”02之 CopyOnWriteArrayList
概要 本章是"JUC系列"的CopyOnWriteArrayList篇.接下来,会先对CopyOnWriteArrayList进行基本介绍,然后再说明它的原理,接着通过代码去分析, ...
- Java多线程系列--“JUC集合”03之 CopyOnWriteArraySet
概要 本章是JUC系列中的CopyOnWriteArraySet篇.接下来,会先对CopyOnWriteArraySet进行基本介绍,然后再说明它的原理,接着通过代码去分析,最后通过示例更进一步的了解 ...
- Java多线程系列--“JUC集合”04之 ConcurrentHashMap
概要 本章是JUC系列的ConcurrentHashMap篇.内容包括:ConcurrentHashMap介绍ConcurrentHashMap原理和数据结构ConcurrentHashMap函数列表 ...
- Java多线程系列--“JUC集合”06之 ConcurrentSkipListSet
概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...
- Java多线程系列--“JUC集合”07之 ArrayBlockingQueue
概要 本章对Java.util.concurrent包中的ArrayBlockingQueue类进行详细的介绍.内容包括:ArrayBlockingQueue介绍ArrayBlockingQueue原 ...
- Java多线程系列--“JUC集合”08之 LinkedBlockingQueue
概要 本章介绍JUC包中的LinkedBlockingQueue.内容包括:LinkedBlockingQueue介绍LinkedBlockingQueue原理和数据结构LinkedBlockingQ ...
- Java多线程系列--“JUC集合”09之 LinkedBlockingDeque
概要 本章介绍JUC包中的LinkedBlockingDeque.内容包括:LinkedBlockingDeque介绍LinkedBlockingDeque原理和数据结构LinkedBlockingD ...
随机推荐
- windows命令行大全
命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一 ...
- November 18th 2016 Week 47th Friday
Get a livelihood and then practise virtue. 先谋生,而后修身. If you can't earn a life, all the things you ha ...
- [EffectiveC++]item28:避免返回handles指向对象内部成分
可以先参考一个帖子:http://bbs.csdn.net/topics/390731394?page=1
- 【原创】python requests 库底层Sockets处于close_wait状态
以前对于Requests库只是简单是使用,在现在公司的后台中,有多个接口是直接使用requests.get .post之类的方法来做的,进行过一段时间的压力测试,发现性能低的可怜,且linux服务器有 ...
- 如何运行vue项目(从gethub上download的开源项目)
前提:入坑vue.js,从GitHub上download一个vue.js的开源项目,发现不知如何在浏览器运行,通过查阅网上教程,发现网上的很多是教你怎么新建项目,并没有一个是教如何打开已有的项目.自已 ...
- RAC with asm on AIX, ORA-01114 error,with "gipcretAuthFail (22) " in ocssd.log
I/O Errors in Alert log with ORA-29701, with "gipcWait failed with 16" in trace (文档 ID 149 ...
- 在任务管理器中显示所有CPU内核性能
在Windows7"任务管理器"的”性能“选项卡默认显示所有的CPU内核性能 在Windows10中可以通过设置来实现效果
- kendo ui - DropDownList 下拉列表系列
kendo-ui 官网:https://www.telerik.com/documentation 初始化 grid: 引入文件: <link rel="stylesheet" ...
- Kafka设计解析(十五)Kafka controller重设计
转载自 huxihx,原文链接 Kafka controller重设计 目录 一.Controller是做什么的 二.Controller当前设计 三.Controller组成 四.Controlle ...
- Mysql(压缩包)下载与安装
第一步:百度搜索 MySQL 点击官网进入 或者复制链接进入下载页面:https://downloads.mysql.com/archives/community/ 第二步:选择自己需要的 ...