简体   繁体   English

HashMap.put无法正常工作

[英]HashMap.put isn't working as expected

I'm trying to save a HashMap in another hashmap. 我正在尝试将HashMap保存在另一个哈希图中。

The problem comes when I save one key and value in the inner hashmap. 当我在内部哈希图中保存一个键和值时,就会出现问题。

When I try to recover the value, always return null. 当我尝试恢复该值时,始终返回null。

It's like HashMap doesnt work... Why?? 就像HashMap无法正常工作...为什么?

I try to create a global variable protected and final.. and nothing :( 我试图创建一个全局变量保护和最终..没有什么:(

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

The output is: 输出为:

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

Edit!: Thanks you, but when im triying to recover a value, using 编辑!:谢谢,但是当我试图恢复值时,使用

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

Test Class: 测试类别:

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

Output: 输出:

value
4
value
88

I only get the last value!! 我只得到最后的价值! why?!!! 为什么?!!! Thanks you a lot :) im a noob! 非常感谢您:)我是菜鸟! :( :(

Edit2 !! 编辑2!

The XML is like that (made with emf tool editor) 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>

You're not testing for the same thing you're putting in: 您没有测试要放入的相同内容:

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

atrib.getName() isn't the number 0, is it? atrib.getName()不是数字0,对吗? If you change your code to: 如果将代码更改为:

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

you'll find it can recover the value with no problems. 您会发现它可以毫无问题地恢复价值。 What did you expect a.get(0) to do? 您期望a.get(0)做什么? Were you perhaps expecting it to return the first element within the map? 您是否希望它返回地图中的第一个元素? Maps don't work like that - the get() method fetches by key . 地图不是那样工作的get()方法通过key获取。

EDIT: If you're setting multiple entries with a key of value , that suggests you've got multiple attributes with a name of value . 编辑:如果您要设置多个条目的一个关键value ,这表明你有多个属性用的名称value Note that you've got two loops: 请注意,您有两个循环:

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

and: 和:

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

So if you've got multiple elements all with a value attribute then yes , the earlier values will be overwritten by the later ones. 因此,如果您有多个具有value属性的所有元素,则yes ,之前的值将被后面的值覆盖。

I suspect you have XML like this: 我怀疑您有这样的XML:

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

... but you haven't shown us your XML, so we can't really say for certain. ...但是您尚未向我们展示您的XML,因此我们不能确定地说。

EDIT: Now that we've seen your XML, it's not clear why you're using the attribute name at all, or why you want maps at all. 编辑:既然我们已经看到了XML,现在还不清楚为什么要使用属性名,或者为什么要使用映射。 It looks to me like you really want a List<List<String>> : 在我看来,您确实想要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"));
    }
}

This will be much simpler than using maps etc. 这将是比使用地图等简单得多

最终的杂音不能覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM