![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
Hi all
Learning java and I'm having problems using an iterator on a hashmap. Here's the situation: I've got a hashmap consisting of an Identifier (class in the application) as the key, and a Dvd object as the value. In the instructions, I have to create the method "public Iterator getDvdKeyIterator()", which will allow me to traverse through the keys. What I've got so far for the method is: public Iterator getDvdKeyIterator(){
entries = dvds.keySet();
iterator = entries.iterator();
return(iterator);
}The second problem is how to call this in another class. I can't create an Iterator object because it's not a concrete class (it tells me), and I can't create an Identifier (which is what the key is) and cast the result of calling this method because it gives me a Cast Error: Identifier dvdID = new Identifier(); dvdID = (Identifier) database.getDvdKeyIterator(); Thanks for any tips or hints!!
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
Last edited by Hockeyman; Jun 14th, 2005 at 10:14 AM. Reason: fixed code tags |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
You do no what a iterator is dont you i hope you do.
well i will assume you do know, therefore chaning an iterator to your custom object is pointless, and wont work. The whole point of the iterator is to go through the map so you can go while (iterator.hasNext()){
<do something>
}so i think you would need... Identifier dvdID = new Identifier(); Iterator itDVD = database.getDvdKeyIterator();
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#3 |
|
Programmer
|
Okay - I might be a bit hazy on iterators.
If I write the code in getDvdKeyIterator(), it should return an iterator of the keys of the hashmap I've created. So when I call Iterator dvdIt = database.getDvdKeyIterator(); how do I get the value of the Identifier key that I'm trying to get? From what I understand, the .next() method returns the next element in the iteration. Maybe I'm just not understanding what exactly it returns - I assumed that because it's iterating over a set of keys, it will return the value of that key when I call .next(). This obviously isn't the case because when I write Identifier dvdID = new Identifier(); Iterator dvdIt = database.getDvdKeyIterator(); dvdID = dvdIt.next(); At the end of the day, what I need is the key (which is an Identifier) for each Dvd in the hashmap using the iterator... (sorry if I'm seeming a bit obtuse here - I spent a couple of hours trying to figure this out last night and I'm completely stuck - I'm sure it's just some relatively simple conceptual thing I'm missing...)
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
okay i just looked it up and i think this should work
<hashmapname>.keySet().iterator(); returns an iterator or the keys within the hashmap <haspmap>.keySet(); returns a set and you can cast iterator on set Good luckhttp://java.sun.com/j2se/1.5.0/docs/...l/HashMap.html
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|