简体   繁体   English

用Java映射

[英]Mapping in java

I have a map code in java.This is my following code. 我在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>();
}

Here i am able to get the output.My problem is i need match each key values to separate strings for further use in my code.How to i can get each key value in separate strings.Please help me. 在这里我能够得到输出。我的问题是我需要将每个键值匹配到单独的字符串以在我的代码中进一步使用。如何获得每个键值在单独的字符串中。请帮助我。

This ill allow you to iterate through your maps keyset. 这种情况使您无法遍历地图键集。 However, depending on what you need Guava has a bi-directional map implementation which could be useful in your case. 但是,根据您的需要,Guava具有双向地图实现,在您的情况下可能会有用。

for(String key:map.keySet())
  System.out.println("key: "+key+" value: "+map.get(key));

Taking a guess at your meaning, you want to populate variables corresponding to the keys 猜测一下您的意思,您想填充与键对应的变量

String type; // gets the value "One"
String name; // gets the value "Two"
String name; // gets the value "Three"

Now I assume that simple code such as 现在,我假设像

type = map.get("type");

is unacceptable, or you surely would not ask the question, so you want some programmatic way of iterating the set of properties? 是不可接受的,还是您肯定不会问这个问题,因此您需要某种编程方式来迭代属性集?

In which case look at the Reflection APIs. 在这种情况下,请查看反射API。

Please confirm that this is the problem you're trying to solve, and have a look at the APIs, if you then have specific questions follow up ... 请确认这是您要解决的问题,并查看API,如果您还有其他问题需要跟进...

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

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