HashMap<string, class<?>>怎么赋值
时间: 2023-06-04 15:05:13 浏览: 111
对于这个问题,我们可以使用put方法对HashMap进行赋值,具体方法如下:
HashMap<String, Class<?>> hashMap = new HashMap<>();
hashMap.put("key", value);
其中,key表示要存储的键名,value表示要存储的值。Class<?>表示可以存储任意类型的Class对象。
相关问题
HashMap<string, clasa<?>>怎么赋值
您好,HashMap<string, clasa<?>>的赋值方式如下:
HashMap<String, Class<?>> map = new HashMap<>();
map.put("key1", String.class);
map.put("key2", Integer.class);
这样就可以将String和Integer的Class对象存入HashMap中,并通过对应的key进行存取了。希望能够帮到您!
在Java中用Dom4j把这一段xml内容转成Map对象:<?xml version="1.0" encoding="UTF-8"?> <java version="1.6.0_26" class="java.beans.XMLDecoder"> <object class="java.util.HashMap"> <void method="put"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method="put"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method="put"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method="put"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method="put"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method="put"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method="put"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method="put"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method="put"> <string>att_20160511130031927equg</string> <string></string> </void> <void method="put"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method="put"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>
您可以使用以下代码将该XML转换为Map对象:
```java
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlToMap {
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <java version=\"1.6.0_26\" class=\"java.beans.XMLDecoder\"> <object class=\"java.util.HashMap\"> <void method=\"put\"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method=\"put\"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method=\"put\"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method=\"put\"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method=\"put\"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method=\"put\"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method=\"put\"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method=\"put\"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method=\"put\"> <string>att_20160511130031927equg</string> <string></string> </void> <void method=\"put\"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method=\"put\"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>";
Map<String, String> map = xmlToMap(xml);
System.out.println(map);
}
public static Map<String, String> xmlToMap(String xml) throws Exception {
Map<String, String> map = new HashMap<>();
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(xml));
Element root = document.getRootElement().element("object");
for (Element element : root.elements("void")) {
Element keyElement = element.element("string");
Element valueElement = element.elements().get(1);
String key = keyElement.getText();
String value = valueElement == null ? null : valueElement.getText();
map.put(key, value);
}
return map;
}
}
```
该代码使用Dom4j库解析XML,并将其转换为Map对象。输出结果为:
```
{att_20160719180419281NEk9=cx, att_20160511094248589ZKYq=cnmm, att_20161018153614188aTus=zsx;fj, att_20160422110906494XuEf=null, att_20160511130210612UULE=fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy, att_20160719180542840LnFm=, att_20160511130025203ssle=zdgsszta, att_20160810131359346N0ra=szjys, att_20160511130031927equg=, att_20160422110456036qp7Y=null, att_20160422110543585f7iI=cpjbxx}
```
阅读全文