Neil
Neil

Reputation: 1853

Retrieving hashmap values in java

I wrote below code to retrieve values in hashmap. But it didnt work.

HashMap<String, String> facilities = new HashMap<String, String>();

Iterator i = facilities.entrySet().iterator();

while(i.hasNext())
{
    String key = i.next().toString();  
    String value = i.next().toString();
    System.out.println(key + " " + value);
}

I modified the code to include SET class and it worked fine.

Set s= facilities.entrySet();
Iterator it = facilities.entrySet().iterator();
while(it.hasNext())
{
    System.out.println(it.next());
}

Can anyone guide me what went wrong in above code without SET class??

P.S - I do not have much programming exp and started using java recently

Upvotes: 1

Views: 34198

Answers (5)

Sudhanshu Umalkar
Sudhanshu Umalkar

Reputation: 4202

String key;
for(final Iterator iterator = facilities.keySet().iterator(); iterator.hasNext(); ) {<BR>
   key = iterator.next();<BR>
   System.out.println(key + " : " + facilities.get(key));<BR>

for (Entry<String, String> entry : facilities.entrySet()) {<BR>
System.out.println(entry.getKey() + " : " + entry.getValue();<BR>
}

Upvotes: 0

Kai
Kai

Reputation: 39632

You are calling next() two times.

Try this instead:

while(i.hasNext())
{
    Entry e = i.next();
    String key = e.getKey();  
    String value = e.getValue();
    System.out.println(key + " " + value);
}

In short you could also use the following code (which also keeps the type information). Using Iterator is pre-Java-1.5 style somehow.

for(Entry<String, String> entry : facilities.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key + " " + value);
}

Upvotes: 11

user2030471
user2030471

Reputation:

You're calling i.next() more than once in the loop. I think this is causing the trouble.

You can try this:

HashMap<String, String> facilities = new HashMap<String, String>();
Iterator<Map.Entry<String, String>> i = facilities.entrySet().iterator();
Map.Entry<String, String> entry = null;
while (i.hasNext()) {
    entry = i.next();
    String key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key + " " + value);
}

Upvotes: 0

vikingsteve
vikingsteve

Reputation: 40378

Iterator i = facilities.keySet().iterator();

while(i.hasNext())
{
    String key = i.next().toString();  
    String value = facilities.get(key);
    System.out.println(key + " " + value);
}

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55589

The problem is you're calling i.next() to get the key, then you call it again to get the value (the value of the next entry).

Another problem is you use toString on the one of the Entry's, which is not the same as getKey or getValue.

You need to do something like:

Iterator<Entry<String, String>> i = facilities.entrySet().iterator();
...
while (...)
{
   Entry<String, String> entry = i.next();
   String key = entry.getKey();  
   String value = entry.getValue();
   ...
}

Upvotes: 2

Related Questions