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
-
2what error are you getting with the hashmap, i.e what is displayed ?Prateek– Prateek2013年02月22日 10:38:21 +00:00Commented Feb 22, 2013 at 10:38
-
1What did you expect and what did you experience instead?Behe– Behe2013年02月22日 10:46:34 +00:00Commented 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?Neil– Neil2013年02月22日 11:00:46 +00:00Commented Feb 22, 2013 at 11:00
5 Answers 5
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);
}
Comments
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
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
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
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>
}