Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2005, 10:13 AM   #1
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
Question Using Iterator on a HashMap

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);
	}
where entries is a Set and dvds is the HashMap. I think something in there is wrong, but I can't find anything in my textbook or the tutorials to explain it.

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();
where database is the database proxy that stores all the dvd items.

Thanks for any tips or hints!!
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~


Last edited by Hockeyman; Jun 14th, 2005 at 10:14 AM. Reason: fixed code tags
Hockeyman is offline   Reply With Quote
Old Jun 14th, 2005, 11:00 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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
Berto is offline   Reply With Quote
Old Jun 14th, 2005, 1:26 PM   #3
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
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();
it doesn't work.

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

Hockeyman is offline   Reply With Quote
Old Jun 15th, 2005, 4:31 AM   #4
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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 luck

http://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
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC