• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterator in HashMap  RSS feed

 
Mansi Vyas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the equivalent of iterator in HashMap? How do I loop through the objects in the HashMap?
Mansi
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have three views which are Collections you can iterate through: keySet(), values(), and entrySet(). Many people do somethign like this:

However, this is a big waste of time, performaing a get() on every singe key, which includes a hashCode() calculation. Completely unnecessary. If you want to get both keys and values, do something like:

If you really only need the keys, use keySet(); if you only need values, use values(); if you need both, use entrySet().
 
Mansi Vyas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!