package demo;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xiuye.util.cls.TypeUtil;
import com.xiuye.util.log.LogUtil;
public class Demo11 {
private static class Node {
private int id;
private String name;
private int pid;
private List<Node> sons;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public List<Node> getSons() {
return sons;
}
public void setSons(List<Node> sons) {
this.sons = sons;
}
@Override
public String toString() {
return "Node [id=" + id + ", name=" + name + ", pid=" + pid + ", sons=" + sons + "]";
}
}
public static void main(String[] args) {
// 存储: id => node
// id 主键 唯一
Map<Integer, Node> map = TypeUtil.createMap();
Node n = new Node();
n.setId(1);
n.setName(UUID.randomUUID().toString());
n.setPid(0);
map.put(n.getId(), n);
n = new Node();
n.setId(2);
n.setName(UUID.randomUUID().toString());
n.setPid(0);
map.put(n.getId(), n);
n = new Node();
n.setId(3);
n.setName(UUID.randomUUID().toString());
n.setPid(1);
map.put(n.getId(), n);
n = new Node();
n.setId(4);
n.setName(UUID.randomUUID().toString());
n.setPid(1);
map.put(n.getId(), n);
n = new Node();
n.setId(5);
n.setName(UUID.randomUUID().toString());
n.setPid(1);
map.put(n.getId(), n);
n = new Node();
n.setId(6);
n.setName(UUID.randomUUID().toString());
n.setPid(5);
map.put(n.getId(), n);
n = new Node();
n.setId(7);
n.setName(UUID.randomUUID().toString());
n.setPid(5);
map.put(n.getId(), n);
//pid => List<SonNode>
Map<Integer, List<Node>> pIdMap = TypeUtil.createMap();
for (Entry<Integer, Node> en : map.entrySet()) {
int id = en.getKey();
Node node = en.getValue();
List<Node> nodes = pIdMap.get(node.getPid());
if(Objects.isNull(nodes)) {
nodes = TypeUtil.createList();
pIdMap.putIfAbsent(node.getPid(), nodes);
}
nodes.add(node);
}
LogUtil.log(pIdMap);
Gson g = new GsonBuilder().setPrettyPrinting().create();
LogUtil.log(g.toJson(pIdMap));
//根据id pid 转成树形结构
//转换算法核心:由叶子节到根节点进行处理,也即由下到上处理
List<Node> nodes = TypeUtil.createList();
for (Entry<Integer, Node> en : map.entrySet()) {
int id = en.getKey();
Node node = en.getValue();
Node pNode = map.get(node.getPid());
//含有父节点就是子节点
if(Objects.nonNull(pNode)) {
List<Node> sons = pNode.getSons();
if(Objects.isNull(sons)) {
sons = TypeUtil.createList();
pNode.setSons(sons);
}
sons.add(node);
}else {//不是子节点就是父节点
nodes.add(node);
}
}
LogUtil.log(nodes);
LogUtil.log(g.toJson(nodes));
}
}
{0=[Node [id=1, name=5dcdf1bc-ebdb-40fd-8ce5-cc35969d8ef4, pid=0, sons=null], Node [id=2, name=b4679cfd-1741-44e9-b887-3f3ad850502e, pid=0, sons=null]], 1=[Node [id=3, name=38d90ae6-4ec7-4209-b854-a14731ac7bc5, pid=1, sons=null], Node [id=4, name=77509bf4-8d79-4140-93d9-9fb7b6d6573e, pid=1, sons=null], Node [id=5, name=2696726e-75bd-4f47-9e14-4dc008a064ab, pid=1, sons=null]], 5=[Node [id=6, name=9d6b4ab9-0eba-4c1b-bc12-438baf0973ff, pid=5, sons=null], Node [id=7, name=4e786e78-d5d7-4c4a-a544-cbb2e86fed60, pid=5, sons=null]]}
{
"0": [
{
"id": 1,
"name": "5dcdf1bc-ebdb-40fd-8ce5-cc35969d8ef4",
"pid": 0
},
{
"id": 2,
"name": "b4679cfd-1741-44e9-b887-3f3ad850502e",
"pid": 0
}
],
"1": [
{
"id": 3,
"name": "38d90ae6-4ec7-4209-b854-a14731ac7bc5",
"pid": 1
},
{
"id": 4,
"name": "77509bf4-8d79-4140-93d9-9fb7b6d6573e",
"pid": 1
},
{
"id": 5,
"name": "2696726e-75bd-4f47-9e14-4dc008a064ab",
"pid": 1
}
],
"5": [
{
"id": 6,
"name": "9d6b4ab9-0eba-4c1b-bc12-438baf0973ff",
"pid": 5
},
{
"id": 7,
"name": "4e786e78-d5d7-4c4a-a544-cbb2e86fed60",
"pid": 5
}
]
}
[Node [id=1, name=5dcdf1bc-ebdb-40fd-8ce5-cc35969d8ef4, pid=0, sons=[Node [id=3, name=38d90ae6-4ec7-4209-b854-a14731ac7bc5, pid=1, sons=null], Node [id=4, name=77509bf4-8d79-4140-93d9-9fb7b6d6573e, pid=1, sons=null], Node [id=5, name=2696726e-75bd-4f47-9e14-4dc008a064ab, pid=1, sons=[Node [id=6, name=9d6b4ab9-0eba-4c1b-bc12-438baf0973ff, pid=5, sons=null], Node [id=7, name=4e786e78-d5d7-4c4a-a544-cbb2e86fed60, pid=5, sons=null]]]]], Node [id=2, name=b4679cfd-1741-44e9-b887-3f3ad850502e, pid=0, sons=null]]
[
{
"id": 1,
"name": "5dcdf1bc-ebdb-40fd-8ce5-cc35969d8ef4",
"pid": 0,
"sons": [
{
"id": 3,
"name": "38d90ae6-4ec7-4209-b854-a14731ac7bc5",
"pid": 1
},
{
"id": 4,
"name": "77509bf4-8d79-4140-93d9-9fb7b6d6573e",
"pid": 1
},
{
"id": 5,
"name": "2696726e-75bd-4f47-9e14-4dc008a064ab",
"pid": 1,
"sons": [
{
"id": 6,
"name": "9d6b4ab9-0eba-4c1b-bc12-438baf0973ff",
"pid": 5
},
{
"id": 7,
"name": "4e786e78-d5d7-4c4a-a544-cbb2e86fed60",
"pid": 5
}
]
}
]
},
{
"id": 2,
"name": "b4679cfd-1741-44e9-b887-3f3ad850502e",
"pid": 0
}
]