xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
package com.nuvole.util;
 
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
 
import java.util.ArrayList;
import java.util.HashMap;
 
/**
 * @author ChenLong
 * @version 1.0
 * @ClassName TreeUtilTest
 * @date 2019/4/23 19:04
 */
@Slf4j
public class TreeUtilTest {
    @Test
    public void convert2Tree() throws Exception {
        
        ArrayList list = new ArrayList();
        //list.add(new Node(1L, 0L, "1", ""));
        list.add(new Node(2L, 1L, "1.1", ""));
        list.add(new Node(3L, 1L, "1.2", ""));
        list.add(new Node(4L, 3L, "1.2.1", ""));
 
        HashMap<String, String> mapping = new HashMap<>();
        mapping.put("byReplace", "label");
 
        log.info(JSON.toJSONString(TreeUtil.convert2Tree(list ,"")));
        log.info(JSON.toJSONString(TreeUtil.convert2Tree(list ,mapping)));
 
    }
 
    class Node{
 
        public Node(Long id,Long pid,String name,String byReplace) {
            this.id = id;
            this.pid = pid;
            this.name = name;
            this.byReplace = byReplace;
        }
 
        private Long id;
        private Long pid;
        private String name;
        private String byReplace;
 
        public Long getId() {
            return id;
        }
 
        public void setId(Long id) {
            this.id = id;
        }
 
        public Long getPid() {
            return pid;
        }
 
        public void setPid(Long pid) {
            this.pid = pid;
        }
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
 
        public String getByReplace() {
            return byReplace;
        }
 
        public void setByReplace(String byReplace) {
            this.byReplace = byReplace;
        }
    }
 
}