MapDFS.java
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.skua.tool.dfs;
import lombok.Data;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* <pre>
* <请输入描述信息>
* </pre>
*
* @author sonin
* @version 1.0 2023/4/16 8:54
*/
@Data
public class MapDFS {
// 定义基础常量
private String EMPTY = "";
private String id = "id";
private String parentId = "parentId";
private String name = "name";
private String children = "children";
private String orderNum = "orderNum";
private List<String> parentList = new ArrayList<String>() {{
add("");
add("null");
}};
public MapDFS() {
}
public MapDFS(String id, String parentId, String name, String orderNum, List<String> parentList) {
if (id != null && id.length() != 0) {
this.id = id;
}
if (parentId != null && parentId.length() != 0) {
this.parentId = parentId;
}
if (name != null && name.length() != 0) {
this.name = name;
}
if (orderNum != null && orderNum.length() != 0) {
this.orderNum = orderNum;
}
if (parentList != null && !parentList.isEmpty()) {
this.parentList = parentList;
}
}
private LinkedList<LinkedList<Map<String, Object>>> routeList = new LinkedList<>();
private void backtrack(List<Map<String, Object>> tree, LinkedList<Map<String, Object>> track) {
if (!track.isEmpty() && ((List) track.getLast().get(children)).isEmpty()) {
routeList.add(new LinkedList<>(track));
}
for (Map<String, Object> item : tree) {
track.addLast(item);
backtrack((List) item.get(children), track);
track.removeLast();
}
}
public LinkedList<LinkedList<Map<String, Object>>> getRouteList(List<Map<String, Object>> tree) {
this.backtrack(tree, new LinkedList<>());
return routeList;
}
public List<Map<String, Object>> buildTree(List<Map<String, Object>> mapList) {
List<Map<String, Object>> tree = new ArrayList<>();
for (Map<String, Object> k : mapList) {
k.put(children, new ArrayList<>());
for (Map<String, Object> v : mapList) {
if (k.get(id).equals(v.get(parentId))) {
((List) k.get(children)).add(v);
}
}
// 提取出父节点
String parentIdStr = EMPTY + k.get(parentId);
if (parentList.contains(parentIdStr)) {
tree.add(k);
}
}
// 排序
sortFunc(tree);
return tree;
}
/**
* <pre>
* 先按照orderNum升序排序,再按照name排序
* </pre>
* @param list
* @author sonin
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
private void sortFunc(List<Map<String, Object>> list) {
if (list != null && !list.isEmpty()) {
list.sort((o1, o2) -> {
if (o1.get(orderNum) == null || o2.get(orderNum) == null) {
return (EMPTY + o1.get(name)).compareTo(EMPTY + o2.get(name));
} else {
if (o1.get(orderNum).equals(o2.get(orderNum))) {
return (EMPTY + o1.get(name)).compareTo(EMPTY + o2.get(name));
} else {
return (Integer.valueOf(EMPTY + o1.get(orderNum))).compareTo(Integer.valueOf(EMPTY + o2.get(orderNum)));
}
}
});
for (Map<String, Object> item : list) {
if (item.get(children) instanceof List) {
sortFunc((List) item.get(children));
}
}
}
}
}