• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Map.Entry Details help

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I would like to know the advantages of using Map.Entry it is used to get the Set view of the map. But I need much more details regarding the same.
I saw an example in which there is a HashMap and since Iterator cannot be used on HaspMap it is converted into Set using entrySet
h_map.entrySet().iterator();
While Iterating, (Map.Entry)iter.next() is used. I would like to know why is that necessary.
Why not a Map be used to get the KeyValue pair. I not getting in the Order in which the values were added into the HashMap too, if I consider that to be the advantage of using Map.entry.

Please help needed.

Thanks
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, HashMap allows you to iterate the keys, the values, or the key/value pairs. Map.entrySet().iterator() is for iterating key/value pairs stored in Map.Entry objects.The reason to use this method is so you don't have to look up the value for each key when iterating keys.

If you need the keys and key/values in the same order as you added them, you'll need to use JDK 1.4's LinkedHashMap class or roll your own.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic