我正在尝试将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));
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
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
Run Code Online (Sandbox Code Playgroud)
编辑!:谢谢你,但是当我试图恢复一个值时,使用
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());
}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
a.recuperarHashMap(0);
a.recuperarHashMap(1);
Run Code Online (Sandbox Code Playgroud)
输出:
value
4
value
88
Run Code Online (Sandbox Code Playgroud)
我只得到最后的价值!! 为什么?!!!非常感谢你:)我是一个菜鸟!:(
编辑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>
Run Code Online (Sandbox Code Playgroud)
你没有测试你投入的同样的东西:
a.put(atrib.getName(), atrib.getValue());
...
System.out.println("Testing:::::"+ a.get(0));
Run Code Online (Sandbox Code Playgroud)
atrib.getName()不是数字0,是吗?如果您将代码更改为:
System.out.println("Testing:::::"+ a.get(atrib.getName()));
Run Code Online (Sandbox Code Playgroud)
你会发现它可以毫无问题地恢复价值.你期望a.get(0)做什么?您是否希望它返回地图中的第一个元素?地图不能像那样工作 - get()方法按键提取.
编辑:如果您使用键设置多个条目value,则表明您有多个名称为的属性value.请注意,您有两个循环:
for (org.jdom2.Element e : hijos2){
guardarAtributos(e,i);
}
Run Code Online (Sandbox Code Playgroud)
和:
for (org.jdom2.Attribute atrib : atributos) {
a.put(atrib.getName(), atrib.getValue());
...
}
Run Code Online (Sandbox Code Playgroud)
因此,如果您有多个元素都具有value属性然后是,则较早的值将被后面的值覆盖.
我怀疑你有这样的XML:
<root>
<child>
<x value="3" />
<y value="1" />
<z value="4" />
</child>
<child>
<x value="88" />
</child>
</root>
Run Code Online (Sandbox Code Playgroud)
...但是你还没有向我们展示你的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"));
}
}
Run Code Online (Sandbox Code Playgroud)
这将是很多比使用地图等简单
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |