bharathi
bharathi

Reputation: 6271

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.

Upvotes: 0

Views: 2556

Answers (2)

djna
djna

Reputation: 55897

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

Upvotes: 0

John Kane
John Kane

Reputation: 4443

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));

Upvotes: 1

Related Questions