1

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

3
  • 2
    what error are you getting with the hashmap, i.e what is displayed ? Commented Feb 22, 2013 at 10:38
  • 1
    What did you expect and what did you experience instead? Commented Feb 22, 2013 at 10:46
  • I am not getting any error. But nothing gets displayed in the output screen. So i googled and used SET class instead. Then it displayed the values. So my question is why didnt it display the value without SET class? Commented Feb 22, 2013 at 11:00

5 Answers 5

11

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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();
   ...
}

Comments

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

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

1 Comment

This will not result in the best performance. And this code is more difficult to read in my opinion.
0

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

Comments

0
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>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.