2013-03-25 86 views
1

我試圖將HashMap保存在另一個HashMap中。HashMap.put沒有按預期工作

當我在內部散列表中保存一個鍵和值時,問題就出現了。

當我嘗試恢復該值時,總是返回null。

這就像HashMap不工作......爲什麼?

我嘗試創建一個全局變量的保護,最終..並沒有什麼:(

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 

編輯!: 謝謝你,但是當IM triying到使用

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 

我只得到了最後一個值!爲什麼?!!!非常感謝你:)我是一個noob! :(

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> 
+1

如果它不」什麼都不放,放什麼? – imulsion 2013-03-25 19:19:48

回答

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")); 
    } 
} 

這將是比簡單使用地圖等

+0

關鍵始終是「價值」。你的提議令人困惑。它將打印最後一個值,但地圖只有一個條目。 – jdb 2013-03-25 19:46:02

+0

@alexxino:請將該代碼編輯到您的問題中 - 並告訴我們它正在打印什麼。 – 2013-03-25 19:46:04

+0

@jdb:不,密鑰是屬性的名稱。 – 2013-03-25 19:46:47

-1

最終varibale不能覆蓋