| Author |
Extracting A Value From Hashmap
|
henri henri
Ranch Hand
Joined: Oct 03, 2005
Posts: 115
|
|
I have my HashMap set up to accept a film title as the key and a JavaBean encapsulating information about the film as a value. I need to get the JavaBean out of the HashMap in order to format and present the information in the JavBean. I get stuck at the following line FilmBean film = container.get(it.next());. This is because the get() method only retrives the key.
|
 |
Seb Mathe
Ranch Hand
Joined: Sep 28, 2005
Posts: 225
|
|
I can be sure because you don't post your FilmContainer code, but wouldn't it be better with : FilmBean film = container.get(it.next()); Note : HashMap.get(aKey) returns the object associated with the key, not the key... And are all the cast statements ((int)film.getLength()...) necessary
|
Regards,<br />Seb<br /> <br />SCJP 1.4
|
 |
henri henri
Ranch Hand
Joined: Oct 03, 2005
Posts: 115
|
|
|
The FilmContainer is a simple class extended from HashMap
|
 |
henri henri
Ranch Hand
Joined: Oct 03, 2005
Posts: 115
|
|
This compiles but does not retrieve each Javabean.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
Right. It retrieves all the keys, because you're iterating over the keySet(). If you want to iterate over all the values, you can use container.values() in place of container.keySet() in the above. You need to slow down a little, take a deep breath, and pay attention more.
|
[Jess in Action][AskingGoodQuestions]
|
 |
henri henri
Ranch Hand
Joined: Oct 03, 2005
Posts: 115
|
|
Thank you so much for your help Mr. Friedman-Hill. It works!! The data in my FilmBeans is printed out. It worked with this code:
|
 |
Seb Mathe
Ranch Hand
Joined: Sep 28, 2005
Posts: 225
|
|
If container is a Map, you must retrieve values, too.
|
 |
 |
|
|
subject: Extracting A Value From Hashmap
|
|
|