从数组Java生成树结构

时间:2015-03-10 18:58:40

标签: java arrays json tree hierarchy

我在java中有一个String数组的ArrayList,名为:

List<String[]> initialList = new ArrayList<String[]>();

这包含以下数据(这是它的一个片段,它实际上包括大约900行,包括Charlie,Delta,Echo,每个都具有与这些相同的结构:

[Alpha, A1, 1, null]
[Alpha, A1, 2, null]
[Alpha, A1, 3, null]
[Alpha, A1, 4, null]
[Alpha, A1, 5, null]
[Alpha, A1, 6, null]
[Alpha, A1, 7, null]
[Alpha, A1, 8, null]
[Alpha, A1, 9, null]
[Alpha, A1, 10, null]
[Alpha, A1, 11, null]
[Alpha, A1, 12, null]
[Alpha, A1, 13, null]
[Alpha, A1, 14, null]
[Alpha, A1, 15, null]
[Alpha, A1, 16, null]
[Alpha, A1, 17, null]
[Alpha, A1, 18, null]
[Alpha, A1, 19, null]
[Alpha, A1, 20, null]
[Alpha, A1, 21, null]
[Alpha, A1, 22, null]
[Alpha, A1, 23, null]
[Alpha, A1, 24, null]
[Alpha, A1, 25, null]
[Alpha, A1, 26, null]
[Alpha, A1, 27, null]
[Alpha, A1, 28, null]
[Alpha, A1, 29, null]
[Alpha, A1, 30, null]
[Alpha, A1, 31, null]
[Alpha, A1, 32, null]
[Alpha, A1, 33, null]
[Alpha, A1, 34, null]
[Alpha, A1, 35, null]
[Alpha, A1, 36, null]
[Alpha, A1, 37, null]
[Alpha, A1, 38, null]
[Alpha, A1, 39, null]
[Alpha, A1, 40, null]
[Alpha, A1, 41, null]
[Alpha, A1, 42, null]
[Alpha, A1, 43, null]
[Alpha, A1, 44, null]
[Alpha, A1, 45, null]
[Alpha, A1, 46, null]
[Alpha, A1, 47, null]
[Alpha, A1, 48, null]
[Alpha, A1, 49, null]
[Alpha, A1, 50, null]
[Alpha, A1, 51, null]
[Alpha, A1, 52, null]
[Alpha, A1, 53, null]
[Alpha, A1, 54, null]
[Alpha, A2, 1, null]
[Alpha, A2, 2, null]
[Alpha, A2, 3, null]
[Alpha, A2, 4, null]
[Alpha, A2, 5, null]
[Alpha, A2, 6, null]
[Alpha, A2, 7, null]
[Alpha, A2, 8, null]
[Alpha, A2, 9, null]
[Alpha, A2, 10, null]
[Alpha, A2, 11, null]
[Alpha, A2, 12, null]
[Alpha, A2, 13, null]
[Alpha, A2, 14, null]
[Alpha, A2, 15, null]
[Alpha, A2, 16, null]
[Alpha, A2, 17, null]
[Alpha, A2, 18, null]
[Alpha, A2, 19, null]
[Alpha, A2, 20, null]
[Alpha, A2, 21, null]
[Alpha, A2, 22, null]
[Alpha, A2, 23, null]
[Alpha, A2, 24, null]
[Alpha, A2, 25, null]
[Alpha, A2, 26, null]
[Alpha, A2, 27, null]
[Alpha, A2, 28, null]
[Alpha, A2, 29, null]
[Alpha, A2, 30, null]
[Alpha, A2, 31, null]
[Alpha, A2, 32, null]
[Alpha, A2, 33, null]
[Alpha, A2, 34, null]
[Alpha, A2, 35, null]
[Alpha, A2, 36, null]
[Alpha, A2, 37, null]
[Alpha, A2, 38, null]
[Alpha, A2, 39, null]
[Alpha, A2, 40, null]
[Alpha, A2, 41, null]
[Alpha, A2, 42, null]
[Alpha, A2, 43, null]
[Alpha, A2, 44, null]
[Alpha, A2, 45, null]
[Alpha, A2, 46, null]
[Alpha, A2, 47, null]
[Alpha, A2, 48, null]
[Alpha, A2, 49, null]
[Alpha, A2, 50, null]
[Alpha, A2, 51, null]
[Alpha, A2, 52, null]
[Alpha, A2, 53, null]
[Alpha, A2, 54, null]
[Alpha, A3, 1, null]
[Alpha, A3, 2, null]
[Alpha, A3, 3, null]
[Alpha, A3, 4, null]
[Alpha, A3, 5, null]
[Alpha, A3, 6, null]
[Alpha, A3, 7, null]
[Alpha, A3, 8, null]
[Alpha, A3, 9, null]
[Alpha, A3, 10, null]
[Alpha, A3, 11, null]
[Alpha, A3, 12, null]
[Alpha, A3, 13, null]
[Alpha, A3, 14, null]
[Alpha, A3, 15, null]
[Alpha, A3, 16, null]
[Alpha, A3, 17, null]
[Alpha, A3, 18, null]
[Alpha, A3, 19, null]
[Alpha, A3, 20, null]
[Alpha, A3, 21, null]
[Alpha, A3, 22, null]
[Alpha, A3, 23, null]
[Alpha, A3, 24, null]
[Alpha, A3, 25, null]
[Alpha, A3, 26, null]
[Alpha, A3, 27, null]
[Alpha, A3, 28, null]
[Alpha, A3, 29, null]
[Alpha, A3, 30, null]
[Alpha, A3, 31, null]
[Alpha, A3, 32, null]
[Alpha, A3, 33, null]
[Alpha, A3, 34, null]
[Alpha, A3, 35, null]
[Alpha, A3, 36, null]
[Alpha, A3, 37, null]
[Alpha, A3, 38, null]
[Alpha, A3, 39, null]
[Alpha, A3, 40, null]
[Alpha, A3, 41, null]
[Alpha, A3, 42, null]
[Alpha, A3, 43, null]
[Alpha, A3, 44, null]
[Alpha, A3, 45, null]
[Alpha, A3, 46, null]
[Alpha, A3, 47, null]
[Alpha, A3, 48, null]
[Alpha, A3, 49, null]
[Alpha, A3, 50, null]
[Alpha, A3, 51, null]
[Alpha, A3, 52, null]
[Alpha, A3, 53, null]
[Alpha, A3, 54, null]
[Alpha, A3, 55, null]
[Alpha, A3, 56, null]
[Alpha, A3, 57, null]
[Alpha, A3, 58, null]
[Alpha, A3, 59, null]
[Alpha, A3, 60, null]
[Alpha, A4, 1, null]
[Alpha, A4, 2, null]
[Alpha, A4, 3, null]
[Alpha, A4, 4, null]
[Alpha, A4, 5, null]
[Alpha, A4, 6, null]
[Alpha, A4, 7, null]
[Alpha, A4, 8, null]
[Alpha, A4, 9, null]
[Alpha, A4, 10, null]
[Alpha, A4, 11, null]
[Alpha, A4, 12, null]
[Alpha, A4, 13, null]
[Alpha, A4, 14, null]
[Alpha, A4, 15, null]
[Alpha, A4, 16, null]
[Alpha, A4, 17, null]
[Alpha, A4, 18, null]
[Alpha, A4, 19, null]
[Alpha, A4, 20, null]
[Alpha, A4, 21, null]
[Alpha, A4, 22, null]
[Alpha, A4, 23, null]
[Alpha, A4, 24, null]
[Alpha, A4, 25, null]
[Alpha, A4, 26, null]
[Alpha, A4, 27, null]
[Alpha, A4, 28, null]
[Alpha, A4, 29, null]
[Alpha, A4, 30, null]
[Alpha, A4, 31, null]
[Alpha, A4, 32, null]
[Alpha, A4, 33, null]
[Alpha, A4, 34, null]
[Alpha, A4, 35, null]
[Alpha, A4, 36, null]
[Alpha, A4, 37, null]
[Alpha, A4, 38, null]
[Alpha, A4, 39, null]
[Alpha, A4, 40, null]
[Alpha, A4, 41, null]
[Alpha, A4, 42, null]
[Alpha, A4, 43, null]
[Alpha, A4, 44, null]
[Alpha, A4, 45, null]
[Alpha, A4, 46, null]
[Alpha, A4, 47, null]
[Alpha, A4, 48, null]
[Alpha, A4, 49, null]
[Alpha, A4, 50, null]
[Alpha, A4, 51, null]
[Alpha, A4, 52, null]
[Alpha, A4, 53, null]
[Alpha, A4, 54, null]
[Alpha, A4, 55, null]
[Alpha, A4, 56, null]
[Alpha, A4, 57, null]
[Alpha, A4, 58, null]
[Alpha, A4, 59, null]
[Alpha, A4, 60, null]
[Bravo, B1, 1, null]
[Bravo, B1, 2, null]
[Bravo, B1, 3, null]
[Bravo, B1, 4, null]
[Bravo, B1, 5, null]
[Bravo, B1, 6, null]
[Bravo, B1, 7, null]
[Bravo, B1, 8, null]
[Bravo, B1, 9, null]
[Bravo, B1, 10, null]
[Bravo, B1, 11, null]
[Bravo, B1, 12, null]
[Bravo, B1, 13, null]
[Bravo, B1, 14, null]
[Bravo, B1, 15, null]
[Bravo, B1, 16, null]
[Bravo, B1, 17, null]
[Bravo, B1, 18, null]
[Bravo, B1, 19, null]
[Bravo, B1, 20, null]
[Bravo, B1, 21, null]
[Bravo, B1, 22, null]
[Bravo, B1, 23, null]
[Bravo, B1, 24, null]
[Bravo, B1, 25, null]
[Bravo, B1, 26, null]
[Bravo, B1, 27, null]
[Bravo, B1, 28, null]
[Bravo, B1, 29, null]
[Bravo, B1, 30, null]
[Bravo, B1, 31, null]
[Bravo, B1, 32, null]
[Bravo, B1, 33, null]
[Bravo, B1, 34, null]
[Bravo, B1, 35, null]
[Bravo, B1, 36, null]
[Bravo, B1, 37, null]
[Bravo, B1, 38, null]
[Bravo, B1, 39, null]
[Bravo, B1, 40, null]
[Bravo, B1, 41, null]
[Bravo, B1, 42, null]
[Bravo, B1, 43, null]
[Bravo, B1, 44, null]
[Bravo, B1, 45, null]
[Bravo, B1, 46, null]
[Bravo, B1, 47, null]
[Bravo, B1, 48, null]
[Bravo, B1, 49, null]
[Bravo, B1, 50, null]
[Bravo, B1, 51, null]
[Bravo, B1, 52, null]
[Bravo, B1, 53, null]
[Bravo, B1, 54, null]
[Bravo, B1, 55, null]
[Bravo, B1, 56, null]
[Bravo, B1, 57, null]
[Bravo, B1, 58, null]
[Bravo, B1, 59, null]
[Bravo, B1, 60, null]
[Bravo, B2, 1, null]
[Bravo, B2, 2, null]
[Bravo, B2, 3, null]
[Bravo, B2, 4, null]
[Bravo, B2, 5, null]
[Bravo, B2, 6, null]
[Bravo, B2, 7, null]
[Bravo, B2, 8, null]
[Bravo, B2, 9, null]
[Bravo, B2, 10, null]
[Bravo, B2, 11, null]
[Bravo, B2, 12, null]
[Bravo, B2, 13, null]
[Bravo, B2, 14, null]
[Bravo, B2, 15, null]
[Bravo, B2, 16, null]
[Bravo, B2, 17, null]
[Bravo, B2, 18, null]
[Bravo, B2, 19, null]
[Bravo, B2, 20, null]
[Bravo, B2, 21, null]
[Bravo, B2, 22, null]
[Bravo, B2, 23, null]
[Bravo, B2, 24, null]
[Bravo, B2, 25, null]
[Bravo, B2, 26, null]
[Bravo, B2, 27, null]
[Bravo, B2, 28, null]
[Bravo, B2, 29, null]
[Bravo, B2, 30, null]
[Bravo, B2, 31, null]
[Bravo, B2, 32, null]
[Bravo, B2, 33, null]
[Bravo, B2, 34, null]
[Bravo, B2, 35, null]
[Bravo, B2, 36, null]
[Bravo, B2, 37, null]
[Bravo, B2, 38, null]
[Bravo, B2, 39, null]
[Bravo, B2, 40, null]
[Bravo, B2, 41, null]
[Bravo, B2, 42, null]
[Bravo, B2, 43, null]
[Bravo, B2, 44, null]
[Bravo, B2, 45, null]
[Bravo, B2, 46, null]
[Bravo, B2, 47, null]
[Bravo, B2, 48, null]
[Bravo, B2, 49, null]
[Bravo, B2, 50, null]
[Bravo, B2, 51, null]
[Bravo, B2, 52, null]
[Bravo, B2, 53, null]
[Bravo, B2, 54, null]
[Bravo, B2, 55, null]
[Bravo, B2, 56, null]
[Bravo, B2, 57, null]
[Bravo, B2, 58, null]
[Bravo, B2, 59, null]
[Bravo, B2, 60, null]
[Bravo, B3, 1, null]
[Bravo, B3, 2, null]
[Bravo, B3, 3, null]
[Bravo, B3, 4, null]
[Bravo, B3, 5, null]
[Bravo, B3, 6, null]
[Bravo, B3, 7, null]
[Bravo, B3, 8, null]
[Bravo, B3, 9, null]
[Bravo, B3, 10, null]
[Bravo, B3, 11, null]
[Bravo, B3, 12, null]
[Bravo, B3, 13, null]
[Bravo, B3, 14, null]
[Bravo, B3, 15, null]
[Bravo, B3, 16, null]
[Bravo, B3, 17, null]
[Bravo, B3, 18, null]
[Bravo, B3, 19, null]
[Bravo, B3, 20, null]
[Bravo, B3, 21, null]
[Bravo, B3, 22, null]
[Bravo, B3, 23, null]
[Bravo, B3, 24, null]
[Bravo, B3, 25, null]
[Bravo, B3, 26, null]
[Bravo, B3, 27, null]
[Bravo, B3, 28, null]
[Bravo, B3, 29, null]
[Bravo, B3, 30, null]
[Bravo, B3, 31, null]
[Bravo, B3, 32, null]
[Bravo, B3, 33, null]
[Bravo, B3, 34, null]
[Bravo, B3, 35, null]
[Bravo, B3, 36, null]
[Bravo, B3, 37, null]
[Bravo, B3, 38, null]
[Bravo, B3, 39, null]
[Bravo, B3, 40, null]
[Bravo, B3, 41, null]
[Bravo, B3, 42, null]
[Bravo, B3, 43, null]
[Bravo, B3, 44, null]
[Bravo, B3, 45, null]
[Bravo, B3, 46, null]
[Bravo, B3, 47, null]
[Bravo, B3, 48, null]
[Bravo, B3, 49, null]
[Bravo, B3, 50, null]
[Bravo, B3, 51, null]
[Bravo, B3, 52, null]
[Bravo, B3, 53, null]

结构如下:Alpha,Bravo,Charlie,Delta和Echo是根节点,它们包含:(A1,A2,A3,A4)(B1,B2,B3,B4)(C1,C2,C3, C4)等......然后每个人都持有一个&#39; n&#39;每个号码持有人名的地点数量。

我需要将它变成一个分层树结构。我尝试过以下方法:

Map<String, Set<String>> myNodes = new LinkedHashMap<String, Set<String>>();

    for(String[] myArray : initialList) {
       String previousNode = null;
       for(String node : myArray) {
          if(myNodes.get(node) == null) {
             myNodes.put(node, new HashSet<String>());
          }


          if(previousNode != null) {
             myNodes.get(previousNode).add(node);
          }

          previousNode = node;

       }
    }

但是这输出如下,正如你所看到的那样是错误的而不是我正在寻找的东西(再次,只是完整输出的片段)。

Alpha : [A2, A1, A4, A3]
A1 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 19, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]
1 : [null]
null : []
2 : [null]
null : []
3 : [null]
4 : [null]
5 : [null]
6 : [null]
7 : [null]
8 : [null]
9 : [null]
10 : [null]
11 : [null]
12 : [null]
13 : [null]
14 : [null]
15 : [null]
16 : [null]
17 : [null]
18 : [null]
19 : [null]
20 : [null]
21 : [null]
22 : [null]
23 : [null]
24 : [null]
25 : [null]
26 : [null]
27 : [null]
28 : [null]
29 : [null]
30 : [null]
31 : [null]
32 : [null]
33 : [null]
34 : [null]
35 : [null]
36 : [null]
37 : [null]
38 : [null]
39 : [null]
40 : [null]
41 : [null]
42 : [null]
43 : [null]
44 : [null]
45 : [null]
46 : [null]
47 : [null]
48 : [null]
49 : [null]
50 : [null]
51 : [null]
52 : [null]
53 : [null]
54 : [null]
A2 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 19, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]
A3 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 59, 58, 57, 56, 19, 55, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 60, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]
55 : [null]
56 : [null]
57 : [null]
58 : [null]
59 : [null]
60 : [null]
A4 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 59, 58, 57, 56, 19, 55, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 60, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]
Bravo : [B4, B3, B2, B1]
B1 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 59, 58, 57, 56, 19, 55, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 60, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]
B2 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 59, 58, 57, 56, 19, 55, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 60, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]
B3 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 59, 58, 57, 56, 19, 55, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 60, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]
B4 : [35, 36, 33, 34, 39, 37, 38, 43, 42, 41, 40, 22, 23, 24, 25, 26, 27, 28, 29, 3, 2, 1, 7, 30, 6, 5, 32, 4, 31, 9, 8, 59, 58, 57, 56, 19, 55, 17, 18, 15, 16, 13, 14, 11, 12, 21, 20, 60, 49, 48, 45, 44, 47, 46, 10, 51, 52, 53, 54, 50]

这是使用Map和LinkedHashMap,所以我也尝试使用TreeMap(不确定这是否是我需要的):

TreeMap<String, Set<String>> myNodes = new TreeMap<String, Set<String>>();

    for(String[] myArray : initialList) {
       String previousNode = null;
       for(String node : myArray) {
          if(myNodes.get(node) == null) {
             myNodes.put(node, new HashSet<String>());
          }


          if(previousNode != null) {
             myNodes.get(previousNode).add(node);
          }

          previousNode = node;

       }
    }

但是出现了以下错误:

Exception in thread "main" java.lang.NullPointerException
    at java.util.TreeMap.getEntry(TreeMap.java:324)
    at java.util.TreeMap.get(TreeMap.java:255)

我需要它如上所述的分级。我很想描述它应该输出什么,但说实话,我不确定,我只知道它需要有一个层次结构并转换为JSON,如下所述。

此外,必须按以下格式将其转换为JSON,因此请记住(同样,实际输出要大得多,这只是一个结构示例:

{
 "name":"Alpha",
 "children":[
   {
     "name":"A1",
     "children":[
        {
           "name":"1",
           "children":[
              {
                 "name":"null",
              },
            ]
           "name":"2",
           "children":[
              {
                 "name":"null",
              },
            ]
           "name":"3",
           "children":[
              {
                 "name":"null",
              },
            ]
        },
     "name":"A2",
     "children":[
        {
           "name":"1",
           "children":[
              {
                 "name":"null",
              },
            ]
           "name":"2",
           "children":[
              {
                 "name":"null",
              },
            ]
           "name":"3",
           "children":[
              {
                 "name":"null",
              },
            ]
        },
    ]   
},

我很感激我能得到的任何帮助,因为我真的很难过。 谢谢!

2 个答案:

答案 0 :(得分:2)

尝试运行以下代码。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


class JsonModel {
    private String name;
    private List<JsonModel> children;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<JsonModel> getChildren() {
        return children;
    }
    public void setChildren(List<JsonModel> children) {
        this.children = children;
    }
    public String toString(){
        return "name: "+name + ", children: {"+children+"}";
    }
}

public class Test{

    public static void main(String a[])
    {
        ArrayList<String[]> initialList= new ArrayList<String[]>();
        initialList.add(new String[]{"Alpha", "A1", "1", null});
        initialList.add(new String[]{"Alpha", "A1", "2", null});
        initialList.add(new String[]{"Alpha", "A2", "1", null});
        initialList.add(new String[]{"Alpha", "A2", "2", null});
        initialList.add(new String[]{"Beta", "A1", "1", null});
        initialList.add(new String[]{"Beta", "A1", "2", null});
        initialList.add(new String[]{"Beta", "A2", "1", null});
        initialList.add(new String[]{"Beta", "A2", "2", null});

        ArrayList<JsonModel> returnList = new ArrayList<JsonModel>();

        HashMap<String,HashMap<String,JsonModel>> tempMap = new HashMap<String, HashMap<String,JsonModel>>();
        HashMap<String,JsonModel> tempMapOuterMost = new HashMap<String, JsonModel>();

        // eg. 1,null
        JsonModel layer3Child = null;

        // eg. A1,{}
        JsonModel layer2Child = null;

        // eg. Alpha,{}
        JsonModel layer1Child = null;

        for(String[] myArray: initialList){

            // to set inner most object eg. 1,null
            layer3Child = new JsonModel();
            layer3Child.setName(myArray[2]);

            JsonModel layer4Child = new JsonModel();
            layer4Child.setName("PersonName");
            ArrayList<JsonModel> layer4Children = new ArrayList<JsonModel>();
            layer4Children.add(layer4Child);
            layer3Child.setChildren(layer4Children);

            // getting object of outermost model eg. Alpha,{}
            if (tempMap.containsKey(myArray[0])){
                layer1Child = tempMapOuterMost.get(myArray[0]);
            } else {
                layer1Child = new JsonModel();
                layer1Child.setName(myArray[0]);
                layer1Child.setChildren(new ArrayList<JsonModel>());
                returnList.add(layer1Child);

                tempMapOuterMost.put(myArray[0], layer1Child);
                tempMap.put(myArray[0], new HashMap<String,JsonModel>());
            }

            // getting the object at layer 2 eg. A1,{}
            if (tempMap.get(myArray[0]).containsKey(myArray[1])){
                layer2Child = tempMap.get(myArray[0]).get(myArray[1]);
            } else {
                layer2Child = new JsonModel();
                layer2Child.setName(myArray[1]);
                layer2Child.setChildren(new ArrayList<JsonModel>());

                layer1Child.getChildren().add(layer2Child);

                tempMap.get(myArray[0]).put(myArray[1], layer2Child);
            }

            layer2Child.getChildren().add(layer3Child);
        }

        System.out.println(returnList);
    }

}

答案 1 :(得分:0)

这是一个非常粗略的基本树实现。它可以使用更多的改进,如字符串构建器和泛型。

此外,您应该看看这个问题和答案:Java tree data-structure?

我还建议您阅读这样的数据结构书:https://books.google.com/books?id=GEJ_Jp6mUpgC&dq=data+structures&source=gbs_navlinks_s

public Tree
{
    public String value;
    public List<Tree> children;

    public Tree(String value)
    {
        children = new ArrayList();
        this.value = value;
    }

    public void AddChild(Tree child)
    {
        children.Add(child);        
    }

    public String ToString()
    {
        String fullString = value;
        for(Tree child : children)
        {
            fullString += "\n";
            fullString += child.ToString();
        }

        return fullString;
    }
}