简体   繁体   中英

Mapping in java

I have a map code in java.This is my following code.

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.

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.

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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