[英]Mapping in java
我在Java中有一个地图代码,这是我的以下代码。
Map<String, String> map = new HashMap<String, String>();
map.put("type", "One");
map.put("name", "Two");
map.put("setup", "Three");
System.out.println("Map Values Before: ");
Set<String> keys = map.keySet();
for (Iterator<String> i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = (String) map.get(key);
Map<String, String> map = new HashMap<String, String>();
}
在这里我能够得到输出。我的问题是我需要将每个键值匹配到单独的字符串以在我的代码中进一步使用。如何获得每个键值在单独的字符串中。请帮助我。
这种情况使您无法遍历地图键集。 但是,根据您的需要,Guava具有双向地图实现,在您的情况下可能会有用。
for(String key:map.keySet())
System.out.println("key: "+key+" value: "+map.get(key));
猜测一下您的意思,您想填充与键对应的变量
String type; // gets the value "One"
String name; // gets the value "Two"
String name; // gets the value "Three"
现在,我假设像
type = map.get("type");
是不可接受的,还是您肯定不会问这个问题,因此您需要某种编程方式来迭代属性集?
在这种情况下,请查看反射API。
请确认这是您要解决的问题,并查看API,如果您还有其他问题需要跟进...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.