HashMap.put未按预期工作

时间:2013-03-25 19:16:41

标签: java map hashmap

我正在尝试将HashMap保存在另一个hashmap中。

当我在内部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

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

Edit2 !!

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>

2 个答案:

答案 0 :(得分: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()方法通过获取。

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

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

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

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

我怀疑你有这样的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"));
    }
}

这比使用地图等更简单

答案 1 :(得分:-1)

最终变量无法覆盖