java编程两种树形菜单结构的转换代码

时间:2022-09-13 12:33:57

首先看看两种树形菜单结构的代码示例。

SingleTreeNode:

java" id="highlighter_117761">
?
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
package com.zzj.tree;
public class SingleTreeNode {
    private int id;
    private int pId;
    private String name;
    public SingleTreeNode() {
    }
    public SingleTreeNode(int id, int pId, String name) {
        this.id = id;
        this.pId = pId;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getpId() {
        return pId;
    }
    public void setpId(int pId) {
        this.pId = pId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
      public String toString() {
        return "SingleTreeNode [id=" + id + ", pId=" + pId + ", name=" + name + "]";
    }
}

这种结构很适合存储到关系型数据库中。

MultiTreeNode:

?
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
package com.zzj.tree;
import java.util.List;
public class MultiTreeNode {
    private int id;
    private String name;
    private List<MultiTreeNode> children;
    public MultiTreeNode() {
    }
    public MultiTreeNode(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public MultiTreeNode(int id, String name, List<MultiTreeNode> children) {
        this.id = id;
        this.name = name;
        this.children = children;
    }
    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 List<MultiTreeNode> getChildren() {
        return children;
    }
    public void setChildren(List<MultiTreeNode> children) {
        this.children = children;
    }
    @Override
      public String toString() {
        return "MultiTreeNode [id=" + id + ", name=" + name + ", children=" + children + "]";
    }
}

两种转换

ToMultiTreeTransformer:

?
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
package com.zzj.tree;
import java.util.ArrayList;
import java.util.List;
public class ToMultiTreeTransformer {
    private List<SingleTreeNode> singleTreeNodes;
    private List<MultiTreeNode> multiTreeNodes = new ArrayList<>();
    public ToMultiTreeTransformer(List<SingleTreeNode> singleTreeNodes) {
        this.singleTreeNodes = singleTreeNodes;
    }
    public List<MultiTreeNode> transform(){
        // 先找出所有的根节点
        for (int i = 0; i < singleTreeNodes.size(); i++) {
            SingleTreeNode singleTreeNode = singleTreeNodes.get(i);
            Boolean isRoot = true;
            for (int j = 0; j < singleTreeNodes.size(); j++) {
                SingleTreeNode temp = singleTreeNodes.get(j);
                if (singleTreeNode.getpId() == temp.getId()) {
                    isRoot = false;
                    break;
                }
            }
            if (isRoot) {
                MultiTreeNode multiTreeNode = new MultiTreeNode(singleTreeNode.getId(), 
                            singleTreeNode.getName());
                multiTreeNodes.add(multiTreeNode);
            }
        }
        // 设置子节点
        for (int i = 0; i < multiTreeNodes.size(); i++) {
            MultiTreeNode multiTreeNode = multiTreeNodes.get(i);
            setChildren(multiTreeNode);
        }
        return multiTreeNodes;
    }
    /**
   * 设置子节点
   * @param multiTreeNode
   */
    private void setChildren(MultiTreeNode multiTreeNode){
        for (int j = 0; j < singleTreeNodes.size(); j++) {
            SingleTreeNode temp = singleTreeNodes.get(j);
            if (temp.getpId() == multiTreeNode.getId()) {
                MultiTreeNode child = new MultiTreeNode(temp.getId(), 
                            temp.getName());
                List<MultiTreeNode> children = multiTreeNode.getChildren();
                if (children == null) {
                    children = new ArrayList<>();
                    multiTreeNode.setChildren(children);
                }
                children.add(child);
                setChildren(child);
            }
        }
    }
}

ToSingleTreeTransformer:

?
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
package com.zzj.tree;
import java.util.ArrayList;
import java.util.List;
public class ToSingleTreeTransformer {
    private List<MultiTreeNode> multiTreeNodes;
    private List<SingleTreeNode> singleTreeNodes = new ArrayList<>();
    public ToSingleTreeTransformer(List<MultiTreeNode> multiTreeNodes) {
        this.multiTreeNodes = multiTreeNodes;
    }
    public List<SingleTreeNode> transform(){
        // 先获取根节点
        for (int i = 0; i < multiTreeNodes.size(); i++) {
            MultiTreeNode multiTreeNode = multiTreeNodes.get(i);
            SingleTreeNode singleTreeNode = new SingleTreeNode(multiTreeNode.getId(), 
                      0, multiTreeNode.getName());
            singleTreeNodes.add(singleTreeNode);
        }
        // 获取子节点
        for (int i = 0; i < multiTreeNodes.size(); i++) {
            MultiTreeNode multiTreeNode = multiTreeNodes.get(i);
            getChildren(multiTreeNode);
        }
        return singleTreeNodes;
    }
    /**
   * 获取子节点
   * @param multiTreeNode
   */
    private void getChildren(MultiTreeNode multiTreeNode){
        for (int i = 0; i < singleTreeNodes.size(); i++) {
            SingleTreeNode temp = singleTreeNodes.get(i);
            if (multiTreeNode.getId() == temp.getId()) {
                List<MultiTreeNode> children = multiTreeNode.getChildren();
                if (children != null) {
                    for (int j = 0; j < children.size(); j++) {
                        MultiTreeNode child = children.get(j);
                        SingleTreeNode singleTreeNode = new SingleTreeNode(child.getId(), 
                                        multiTreeNode.getId(), child.getName());
                        singleTreeNodes.add(singleTreeNode);
                        getChildren(child);
                    }
                }
            }
        }
    }
}

测试

?
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
package com.zzj.tree;
import java.util.ArrayList;
import java.util.List;
public class TreeTransformerTest {
    private final static List<SingleTreeNode> SINGLE_TREE = new ArrayList<SingleTreeNode>();
    static {
        SingleTreeNode China = new SingleTreeNode(1, 0, "中国");
        SINGLE_TREE.add(China);
        SingleTreeNode Hunan = new SingleTreeNode(2, 1, "湖南");
        SINGLE_TREE.add(Hunan);
        SingleTreeNode Changsha = new SingleTreeNode(3, 2, "长沙");
        SINGLE_TREE.add(Changsha);
        SingleTreeNode Hubei = new SingleTreeNode(4, 1, "湖北");
        SINGLE_TREE.add(Hubei);
        SingleTreeNode Wuhan = new SingleTreeNode(5, 4, "武汉");
        SINGLE_TREE.add(Wuhan);
        SingleTreeNode America = new SingleTreeNode(6, 0, "美国");
        SINGLE_TREE.add(America);
        SingleTreeNode California = new SingleTreeNode(7, 6, "加利福尼亚");
        SINGLE_TREE.add(California);
        SingleTreeNode LosAngeles = new SingleTreeNode(8, 7, "洛杉矶");
        SINGLE_TREE.add(LosAngeles);
    }
    public static void main(String[] args) throws Exception {
        ToMultiTreeTransformer multiTreeTransformer = new ToMultiTreeTransformer(SINGLE_TREE);
        List<MultiTreeNode> multiTreeNodes = multiTreeTransformer.transform();
        System.out.println(multiTreeNodes);
        ToSingleTreeTransformer singleTreeTransformer = new ToSingleTreeTransformer(multiTreeNodes);
        List<SingleTreeNode> singleTreeNodes = singleTreeTransformer.transform();
        System.out.println(singleTreeNodes);
    }
}

输出结果:

?
1
2
[MultiTreeNode [id=1, name=中国, children=[MultiTreeNode [id=2, name=湖南, children=[MultiTreeNode [id=3, name=长沙, children=null]]], MultiTreeNode [id=4, name=湖北, children=[MultiTreeNode [id=5, name=武汉, children=null]]]]], MultiTreeNode [id=6, name=美国, children=[MultiTreeNode [id=7, name=加利福尼亚, children=[MultiTreeNode [id=8, name=洛杉矶, children=null]]]]]]
[SingleTreeNode [id=1, pId=0, name=中国], SingleTreeNode [id=6, pId=0, name=美国], SingleTreeNode [id=2, pId=1, name=湖南], SingleTreeNode [id=3, pId=2, name=长沙], SingleTreeNode [id=4, pId=1, name=湖北], SingleTreeNode [id=5, pId=4, name=武汉], SingleTreeNode [id=7, pId=6, name=加利福尼亚], SingleTreeNode [id=8, pId=7, name=洛杉矶]]

总结

以上就是本文关于java编程两种树形菜单结构的转换代码的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出!

原文链接:http://blog.csdn.net/zhangzeyuaaa/article/details/54233609