Accessing HashMap using Iterators
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sudarsan_Mukundhan
Posted On:   Thursday, July 21, 2005 06:28 PM

Hi,
I have got a HashMap which needs to be traversed through using Iterators. Anyone has idea of which method i haev to use of iterators??

Re: Accessing HashMap using Iterators

Posted By:   raj_kumar  
Posted On:   Monday, August 1, 2005 05:01 AM

public class test
{
public static void main(String args[])
{
HashMap h = new HashMap();
h.put("1","nag");
h.put("2","raju");

Iterator it = h.iterate();
while(it.hsaNext())
{
System.out.println(h.next());
}
}
}

Re: Accessing HashMap using Iterators

Posted By:   WarnerJan_Veldhuis  
Posted On:   Friday, July 22, 2005 04:40 AM

That depends on what you want to iterate over:

To iterate over the keys: myMap.keySet().iterator()

To iterate over the values: myMap.values().iterator()

To iterate over the key/value-pairs at once: myMap.entrySet().iterator(); You will iterate over each Map.Entry object, which holds a getKey() and a getValue()
About | Sitemap | Contact