/*** 测试 一共有六个Catalog
* 其中:name1下面有三个子节点:name2、name3、name4
* name2下面有两个子节点:name5、name6*/@Testpublic void ListToMap() throwsException {
List list = new ArrayList<>();
Catalog catalog= newCatalog();
String flowId=randomUUID();
catalog.setFlowId(flowId);
catalog.setName("name1");
list.add(catalog);
catalog= newCatalog();
String flowId2=randomUUID();
catalog.setFlowId(flowId2);
catalog.setName("name2");
catalog.setParentId(flowId);
list.add(catalog);
catalog= newCatalog();
String flowId3=randomUUID();
catalog.setFlowId(flowId3);
catalog.setName("name3");
catalog.setParentId(flowId);
list.add(catalog);
catalog= newCatalog();
String flowId4=randomUUID();
catalog.setFlowId(flowId4);
catalog.setName("name4");
catalog.setParentId(flowId);
list.add(catalog);
catalog= newCatalog();
String flowId5=randomUUID();
catalog.setFlowId(flowId5);
catalog.setName("name5");
catalog.setParentId(flowId2);
list.add(catalog);
catalog= newCatalog();
String flowId6=randomUUID();
catalog.setFlowId(flowId6);
catalog.setName("name6");
catalog.setParentId(flowId2);
list.add(catalog);
List 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"
}
]
}
]*/}protectedString randomUUID() {return UUID.randomUUID().toString().replace("-", "");
}