• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map.Entry Details help

 
kalpana Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic