嵌套的TreeMap - 值存储在单个Key中

时间:2015-09-09 13:22:03

标签: java

我是第一次尝试实现TreeMap。在这种情况下,我有以下通过迭代检索的数据:

A
    a
    shape:square
    fill:yes
    size:very large
B
    b
        shape:square
        fill:yes
        size:very large
C
    c
        shape:square
        fill:yes
        size:very large

我需要将这些数据存储在TreeMaps中。一个例子是TreeMap(A,TreeMap(a,Treemap(keySize,sizeAttribute)))

我实现了这个:

Map<String, TreeMap<String, TreeMap<String, String>>> squareRPM = new HashMap<String, TreeMap<String, TreeMap<String, String>>>(); //Everytime it changes object create a new squareRPM
            TreeMap<String, TreeMap<String, String>> component = new TreeMap<String, TreeMap<String, String>>();  
            TreeMap<String, String> CompAttr = new TreeMap<String, String>();    

在迭代中我有:

        for(String figureName : problem.getFigures().keySet()) {  //Iterates through the Name of figures as A, B, C, 1...6
        RavensFigure thisFigure = problem.getFigures().get(figureName);

        for(String objectName : thisFigure.getObjects().keySet()) { //Iterates through components in each A, B, C Ex.  A a 
        RavensObject thisObject = thisFigure.getObjects().get(objectName);


        for(String attributeName : thisObject.getAttributes().keySet()) {  //Iterates through attributes 
        String attributeValue = thisObject.getAttributes().get(attributeName);

        CompAttr.put(attributeName, attributeValue);
        component.put(objectName, CompAttr);
        squareRPM.put(figureName, component);
        }            
    }       

}

        System.out.println(squareRPM.get("A")) ;

我想要的输出是:

[A={a={fill=yes, shape=square, size=very large}}]
[B={b={fill=yes, shape=square, size=very large}}]

但我有这个:

[A={a={fill=yes, shape=square, size=very large},b ={fill=yes, shape=square, size=very large},c={fill=yes, shape=square, size=very large}}]

所有东西都存储在A中。我该如何解决这个问题?

谢谢。

解决了!

        for(String figureName : problem.getFigures().keySet()) {  //Iterates through the Name of figures as A, B, C, 1...6
        RavensFigure thisFigure = problem.getFigures().get(figureName);
        TreeMap<String, TreeMap<String, TreeMap<String, String>>> squareRPM = new TreeMap<String, TreeMap<String, TreeMap<String, String>>>(); //Everytime it changes object create a new squareRPM    

        for(String objectName : thisFigure.getObjects().keySet()) { //Iterates through components in each A, B, C Ex.  A a 
        RavensObject thisObject = thisFigure.getObjects().get(objectName);
        TreeMap<String, TreeMap<String, String>> component = new TreeMap<String, TreeMap<String, String>>();  
        TreeMap<String, String> CompAttr = new TreeMap<String, String>();    

        for(String attributeName : thisObject.getAttributes().keySet()) {  //Iterates through attributes 
        String attributeValue = thisObject.getAttributes().get(attributeName);


        CompAttr.put(attributeName, attributeValue);

        }       
        component.put(objectName, CompAttr);
        squareRPM.put(figureName, component);

//

        for(Entry<String, TreeMap<String, TreeMap<String, String>>> entry : squareRPM.entrySet()) {

              String key = entry.getKey();
              TreeMap<String, TreeMap<String, String>> value = entry.getValue();

              System.out.println(key + " => " + value);

        }
    }       

}

问题是我在创建每个TreeMap&gt;。&gt;

0 个答案:

没有答案