1

我正在尝试将 HashMap 保存在另一个哈希图中。

当我在内部哈希图中保存一个键和值时,问题就来了。

当我尝试恢复值时,总是返回 null。

这就像 HashMap 不起作用......为什么?

我尝试创建一个全局变量 protected 和 final .. 什么都没有:(

protected final Map<Integer,Map> HMG =  new HashMap<Integer,Map>(); //GLOBAL VARAIBLE

    List<org.jdom2.Element> hijos = root.getChildren();
    for(int i=0 ; i < hijos.size(); i++) {
        org.jdom2.Element elem = hijos.get(i);
        String file = elem.getName();
        HMG.put(i, new HashMap<String, String>());
        System.out.println("Hashmap saved to "+ i+" "+file );
        System.out.println(file + i);
        List<org.jdom2.Element> hijos2 =elem.getChildren();
        for (org.jdom2.Element e : hijos2){
            guardarAtributos(e,i);
        }
    }

public void guardarAtributos(org.jdom2.Element elemento,Integer orden) {
    List<org.jdom2.Attribute> atributos=elemento.getAttributes();
     Map<String,String> a =HMG.get(orden);
     for (org.jdom2.Attribute atrib : atributos) {
         a.put(atrib.getName(), atrib.getValue());
         System.out.println("Writting into miniHashMap ===> "+atrib.getName()+" "+" "+atrib.getValue());
         System.out.println("Testing:::::"+ a.get(0));
    }
}

输出是:

Hashmap saved to 0 Number
Number0
Writting into miniHashMap ===> value  3
Testing:::::null
Writting into miniHashMap ===> value  1
Testing:::::null
Writting into miniHashMap ===> value  4
Testing:::::null
Hashmap saved to 1 Number
Number1
Writting into miniHashMap ===> value  88
Testing:::::null

编辑!:谢谢你,但是当我试图恢复一个值时,使用

public void recuperarHashMap(Integer orden){
 Map<String,String> hash= HMG.get(orden);
 for(Entry<String, String> entry: hash.entrySet()) {
     System.out.println(entry.getKey());
     System.out.println(entry.getValue());
 }
}

测试类:

a.recuperarHashMap(0);
a.recuperarHashMap(1);

输出:

value
4
value
88

我只得到最后一个值!!为什么?!!!非常感谢你:) 我是个菜鸟!:(

编辑2!

XML就是这样(用emf工具编辑器制作的)

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:language1="language1">
  <language1:Number id="PI">
    <has value="3"/>
    <has value="1"/>
    <has value="4"/>
  </language1:Number>
  <language1:Number id="888">
    <has value="88"/>
  </language1:Number>
</xmi:XMI>
4

2 回答 2

7

您不是在测试您要放入的相同内容:

a.put(atrib.getName(), atrib.getValue());
...
System.out.println("Testing:::::"+ a.get(0));

atrib.getName()不是数字0吧?如果您将代码更改为:

System.out.println("Testing:::::"+ a.get(atrib.getName()));

你会发现它可以毫无问题地恢复价值。你期望a.get(0)做什么?您是否希望它返回地图中的第一个元素?地图不是这样工作的 - 该get()方法通过key获取。

编辑:如果您使用 键设置多个条目value,则表明您有多个名称为value. 请注意,您有两个循环:

for (org.jdom2.Element e : hijos2){
    guardarAtributos(e,i);
}

和:

for (org.jdom2.Attribute atrib : atributos) {
    a.put(atrib.getName(), atrib.getValue());
    ...
}

因此,如果您有多个元素都具有一个value属性,那么yes,较早的值将被后面的值覆盖。

我怀疑你有这样的 XML:

<root>
  <child>
    <x value="3" />
    <y value="1" />
    <z value="4" />
  </child>
  <child>
    <x value="88" />
  </child>
</root>  

...但是您还没有向我们展示您的 XML,因此我们无法确定。

编辑:现在我们已经看到了您的 XML,尚不清楚您为什么要使用属性名称,或者您为什么需要地图。在我看来你真的想要一个List<List<String>>

List<List<String> elementValues = new ArrayList<List<String>>();

List<Element> elements = root.getChildren();
for (Element element : elements) {
    List<String> values = new ArrayList<String>();
    for (Element child : element.getChildren()) {
        values.add(child.getAttributeValue("value"));
    }
}

这将比使用地图等简单得多

于 2013-03-25T19:19:11.150 回答
-1

最终变量无法覆盖

于 2013-03-25T19:25:41.423 回答