This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Extracting A Value From Hashmap  RSS feed

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The FilmContainer is a simple class extended from HashMap
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles but does not retrieve each Javabean.



 
author and iconoclast
Posts: 24203
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If container is a Map,


you must retrieve values, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!