This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes retrieving keys with a map interator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "retrieving keys with a map interator" Watch "retrieving keys with a map interator" New topic
Author

retrieving keys with a map interator

A Ridolfi
Greenhorn

Joined: Nov 25, 2007
Posts: 10
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

Joined: Sep 29, 2002
Posts: 1683
Have you looked at the API documentation for Map?


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
A Ridolfi
Greenhorn

Joined: Nov 25, 2007
Posts: 10
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

Joined: Nov 08, 2007
Posts: 457
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 ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
A Ridolfi
Greenhorn

Joined: Nov 25, 2007
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
A Ridolfi
Greenhorn

Joined: Nov 25, 2007
Posts: 10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieving keys with a map interator
 
Similar Threads
HashMap vs TreeMap data retrival.
data structure
Servlets with JDBC
Objects in Arraylist
JAXB persistency