cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package cn.ksource.core.util;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/**
 * 多叉树类
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class TreeUtil {
    public Map createTree(List dataList){
        // 节点列表(散列表,用于临时存储节点对象)
        HashMap nodeList = new HashMap();
        // 根节点
        Node root = new Node();
        root.text = "根节点";
        root.id = "1";
        root.parentId = "0";
        root.index = "1";
        // 根据结果集构造节点列表(存入散列表)
        for (Iterator it = dataList.iterator(); it.hasNext();) {
            Map dataRecord = (Map) it.next();
            Node node = new Node();
            node.id = (String) dataRecord.get("id");
            node.text = (String) dataRecord.get("text");
            node.parentId = (String) dataRecord.get("parentId");
            node.index =  dataRecord.get("sortId").toString();
            nodeList.put(node.id, node);
        }
        // 构造无序的多叉树
        Set entrySet = nodeList.entrySet();
        for (Iterator it = entrySet.iterator(); it.hasNext();) {
            Node node = (Node) ((Map.Entry) it.next()).getValue();
            if (node.parentId == null || node.parentId.equals("")) {
                root.addChild(node);
            } else {
                ((Node) nodeList.get(node.parentId)).addChild(node);
            }
        }
        // 输出无序的树形菜单的JSON字符串
        //System.out.println(root.toString());
        // 对多叉树进行横向排序
        root.sortChildren();
        // 输出有序的树形菜单的JSON字符串
        //System.out.println(root.toString());
        Map map = JsonUtil.json2Map(root.toString());
        return map;
    }
    
    public static Map createTreeByListMap(List<Map> dataList){
        Map root = new HashMap();
        root.put("text", "根节点");
        List list = new ArrayList<Map>();
        root.put("children", list);
        //将数据LIST放置到MAP中
        Map<String,Object> dataMap = new LinkedHashMap();
        if(dataList!=null&&dataList.size()>0){
            for(Map map:dataList){
                dataMap.put(map.get("id").toString(),map);
            }
        }
        //组装树
        for(String key:dataMap.keySet()){
            if(((Map)dataMap.get(key)).get("pid")==null||((Map)dataMap.get(key)).get("pid").toString().equals("")||((Map)dataMap.get(key)).get("pid").toString().equals("0")){
                ((List<Map>)root.get("children")).add((Map)dataMap.get(key));
            }else{
                Map parentNode = (Map)dataMap.get(((Map)dataMap.get(key)).get("pid").toString());
                if(parentNode.get("children")==null){
                    List children = new ArrayList<Map>();
                    parentNode.put("children", children);
                }
                ((List<Map>)parentNode.get("children")).add((Map)dataMap.get(key));
            }
        }
        //System.out.println("----------------------"+JsonUtil.map2Json(root));
        return root;
    }
    
    public static List<Map> createTreeByList(List<Map> dataList){
        Map root = new HashMap();
        root.put("text", "根节点");
        List list = new ArrayList<Map>();
        root.put("children", list);
        if(dataList!=null&&dataList.size()>0){
            //将数据LIST放置到MAP中
            Map<String,Object> dataMap = new LinkedHashMap();
            if(dataList!=null&&dataList.size()>0){
                for(Map map:dataList){
                    dataMap.put(map.get("id").toString(),map);
                }
            }
            //组装树
            for(String key:dataMap.keySet()){
                if(((Map)dataMap.get(key)).get("pid")==null||((Map)dataMap.get(key)).get("pid").toString().equals("")||((Map)dataMap.get(key)).get("pid").toString().equals("0")){
                    ((List<Map>)root.get("children")).add((Map)dataMap.get(key));
                }else{
                    Map parentNode = (Map)dataMap.get(((Map)dataMap.get(key)).get("pid").toString());
                    if(parentNode!=null){
                        if(parentNode.get("children")==null){
                            List children = new ArrayList<Map>();
                            parentNode.put("children", children);
                        }
                        ((List<Map>)parentNode.get("children")).add((Map)dataMap.get(key));
                    }
                }
            }
        }
        if(root.get("children")!=null){
            return (List<Map>)root.get("children");
        }else{
            return null;
        }
    }
    
    public static Map createTreeByListHasChildren(List<Map> dataList){
        Map root = new HashMap();
        root.put("text", "根节点");
        List list = new ArrayList<Map>();
        root.put("children", list);
        Map<String,Object> dataMap = new LinkedHashMap();
        if(dataList!=null&&dataList.size()>0){
            for(Map map:dataList){
                List children = new ArrayList<Map>();
                map.put("children", children);
                dataMap.put(map.get("id").toString(),map);
            }
        }
        //组装树
        for(String key:dataMap.keySet()){
            if(((Map)dataMap.get(key)).get("pid")==null||((Map)dataMap.get(key)).get("pid").toString().equals("")||((Map)dataMap.get(key)).get("pid").toString().equals("0")){
                ((List<Map>)root.get("children")).add((Map)dataMap.get(key));
            }else{
                Map parentNode = (Map)dataMap.get(((Map)dataMap.get(key)).get("pid").toString());
                ((List<Map>)parentNode.get("children")).add((Map)dataMap.get(key));
            }
        }
        //System.out.println("----------------------"+JsonUtil.map2Json(root));
        return root;
    }
    
    
    public Map createTree(List<Map> categoryList,List<Map> queryList){
        Map root = new HashMap();
        root.put("text", "根节点");
        List list = new ArrayList<Map>();
        root.put("children", list);
        //将数据LIST放置到MAP中
        Map<String,Object> queryMap = new HashMap();
        if(queryList!=null&&queryList.size()>0){
            for(Map map:queryList){
                queryMap.put(map.get("id").toString(),map);
            }
        }
        //将类型LIST放置到MAP中
        Map<String,Object> categoryMap = new HashMap();
        for(Map map:categoryList){
            categoryMap.put(map.get("id").toString(), map);
        }
        
        //组装树
        for(String key:categoryMap.keySet()) {
            Map node = (Map)categoryMap.get(key);
            if(queryMap.get(key)!=null){
                node.put("dataMap", queryMap.get(key));
            }
            if(node.get("parentId")==null||node.get("parentId").toString().equals("")||node.get("parentId").toString().equals("0")){
                ((List<Map>)root.get("children")).add(node);
            }else{
                Map parentNode = ((Map)categoryMap.get(node.get("parentId").toString()));
                if(parentNode.get("children")==null){
                    List children = new ArrayList<Map>();
                    parentNode.put("children", children);
                }
                ((List<Map>)parentNode.get("children")).add(node);
            }
        }
        //System.out.println("----------------------"+JsonUtil.map2Json(root));
        return root;
    }
    
    
    public Map createTree(List<Map> categoryList,List<Map> queryList,List<Map> lebalList){
        Map root = new HashMap();
        root.put("text", "根节点");
        List list = new ArrayList<Map>();
        root.put("children", list);
        
        Map<String,Object> queryMap = new HashMap();
        if(queryList!=null&&queryList.size()>0){
            for(Map map:queryList){
                queryMap.put(map.get("id").toString(),map);
            }
        }
        
        Map<String,Object> categoryMap = new HashMap();
        for(Map map:categoryList){
            categoryMap.put(map.get("id").toString(), map);
        }
        
        for(String key:categoryMap.keySet()) {
            Map node = (Map)categoryMap.get(key);
            List<String> tempList = new ArrayList<String>();
            for(Map map:lebalList){
                String name = key+"_"+map.get("id").toString();
                if(queryMap.get(name)!=null){
                    tempList.add(((Map)queryMap.get(name)).get("num").toString());
                }else{
                    tempList.add("-");
                }
            }
            node.put("dataList", tempList);
            
            
            if(node.get("parentId")==null||node.get("parentId").toString().equals("")||node.get("parentId").toString().equals("0")){
                ((List<Map>)root.get("children")).add(node);
            }else{
                Map parentNode = ((Map)categoryMap.get(node.get("parentId").toString()));
                if(parentNode!=null){
                    if(parentNode.get("children")==null){
                        List children = new ArrayList<Map>();
                        parentNode.put("children", children);
                    }
                    ((List<Map>)parentNode.get("children")).add(node);
                }
                
                
            }
        }
        System.out.println("----------------------"+JsonUtil.map2Json(root));
        return root;
    }
    
    
    public List<Map> createTreeByCate(List<Map> categoryList,List<Map> dataList){
        //将类型LIST放置到MAP中
        Map<String,Object> categoryMap = new HashMap();
        for(Map map:categoryList){
            List children = new ArrayList<Map>();
            map.put("children", children);
            categoryMap.put(map.get("id").toString(), map);
        }
        
        for(Map data:dataList){
            if(categoryMap.get(data.get("cateId").toString())!=null){
                ((List<Map>)((Map)categoryMap.get(data.get("cateId").toString())).get("children")).add(data);
            }
        }
        return categoryList;
    }
    public List<Map> createTreeByLvlId(List<Map> categoryList,List<Map> dataList){
        //将类型LIST放置到MAP中
        Map<String,Object> categoryMap = new HashMap();
        for(Map map:categoryList){
            List children = new ArrayList<Map>();
            map.put("children", children);
            categoryMap.put(map.get("ID").toString(), map);
        }
        
        for(Map data:dataList){
            if(categoryMap.get(data.get("ID").toString())!=null){
                ((List<Map>)((Map)categoryMap.get(data.get("ID").toString())).get("children")).add(data);
            }
        }
        return categoryList;
    }
}
 
class Node {
    /**
     * 节点编号
     */
    public String id;
    /**
     * 节点内容
     */
    public String text;
    /**
     * 父节点编号
     */
    public String parentId;
    /**
     * 孩子节点列表
     */
    public String index;
    
    public Map dataMap;
    
    public Integer size;
    
    private Children children = new Children();
 
    // 先序遍历,拼接JSON字符串
    public String toString() {
        String result = "{" + "id : '" + id + "'" + ", text : '" + text + "'";
 
        if (children != null && children.getSize() != 0) {
            result += ", children : " + children.toString();
        } else {
            result += ", leaf : true";
        }
 
        return result + "}";
    }
 
    // 兄弟节点横向排序
    public void sortChildren() {
        if (children != null && children.getSize() != 0) {
            children.sortChildren();
        }
    }
 
    // 添加孩子节点
    public void addChild(Node node) {
        this.children.addChild(node);
    }
    
    
    /**
     * 孩子列表类
     */
    class Children {
        private List list = new ArrayList();
 
        public int getSize() {
            return list.size();
        }
 
        public void addChild(Node node) {
            list.add(node);
        }
 
        // 拼接孩子节点的JSON字符串
        public String toString() {
            String result = "[";
            for (Iterator it = list.iterator(); it.hasNext();) {
                result += ((Node) it.next()).toString();
                result += ",";
            }
            result = result.substring(0, result.length() - 1);
            result += "]";
            return result;
        }
 
        // 孩子节点排序
        public void sortChildren() {
            // 对本层节点进行排序
            // 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
            Collections.sort(list, new NodeIDComparator());
            // 对每个节点的下一层节点进行排序
            for (Iterator it = list.iterator(); it.hasNext();) {
                ((Node) it.next()).sortChildren();
            }
        }
    }
 
    /**
     * 节点比较器
     */
    class NodeIDComparator implements Comparator {
        // 按照节点编号比较
        public int compare(Object o1, Object o2) {
            int j1 = Integer.parseInt(((Node) o1).index);
            int j2 = Integer.parseInt(((Node) o2).index);
            return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
        }
    }
    
}