Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing HashMap 1

Status
Not open for further replies.

tvrtko

Programmer
Nov 26, 2003
53
HR
I have some data stored in HashMap and I need to achieve serial access to that data. With HashSet, for instance, I would use iterator. But how to do this with HashMap?

Thansks in advance.
 
Hi

I use [tt]HashMap.entrySet()[/tt] for this purpose, that has an [tt]Iterator[/tt] :
Code:
HashMap hm=new HashMap();
Iterator hmi=hm.entrySet().iterator();
Map.Entry me;

while (hmi.hasNext()) {
  me=(Map.Entry)hmi.next();
  System.out.println(me.getKey()+" : "+me.getValue());
}
( Question to the gurus : is a better way to do this then the above ? )

Feherke.
 
Well, depending on what you're trying to do, HashMap ha two more methods: keySet that returns the keys and values that returns a Collection.

Cheers,
Dian
 
Is it possible to iterate Set in some order - ascending or descending?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top