Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieving keys with a map interator

 
A Ridolfi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an iterator looping through objects on a Map. I can retrieve objects using the Iterator's next(), but I need also to retrieve the key for the current entry, not just the value object. Any suggestions? Any help would be appreciated.

Thanks
Alberto Ridolfi
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the API documentation for Map?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use entrySet() to get a set of map entries (key-value pairs) and iterate over that set. From each entry you can get the key and value.
 
A Ridolfi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger, I would never have thought of that. I'm going to look on the API right now! Thanks! (Of course I have looked, duh)

Jesper, thanks for the suggestion, that solves the problem. I was trying to solve the problem by looking for something like a getKey() on an Iterator or something like that, this entrySet() is a different approach but solves the problem. Thank you.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's a collection of keys, get an iterator from it,
iterate over the keys, ask for the values.

map.keySet().iterator();


[ March 18, 2008: Message edited by: Bill Shirley ]
 
A Ridolfi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's also a good idea Bill, iterating over the keys then getting the object, instead of using the normal Iterator way. Thanks for the suggestion.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For performance reasons I'd suggest using entrySet().

Most keySet() and values() implementations use entries as a backing mechanism, just ignoring the value / key. It's hardly more memory consuming, possibly even less. Secondly, if you use the keySet() you have to "search" the map for each key. With entrySet() it's already there.

So, in most Map implementations:
- memory consumption for entrySet() is either comparable to or better than that of keySet()
- entrySet() is most definitely faster than keySet()

No hard choice for me
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger, I would never have thought of that. I'm going to look on the API right now! Thanks! (Of course I have looked, duh)

The API documentation clearly states that entrySet() and keySet() will both return the keys in the Map, so I thought that you had not read it. Now I realize that you did not understand it. You will need to understand this documentation as it is key (pun intended) to being a good developer.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by A Ridolfi:
... Of course I have looked...

You might be surprised how many people post API-related questions without checking the API documentation. I think Roger's suggestion was a good effort to help.
 
A Ridolfi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the ideas about the approaches.

Roger, my brain gets slow after a long day of programming. Time to rest now. :-)

Rob, thanks for the tip. I will take that in consideration.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic