• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
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.

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.
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic