aspose file tools*
The moose likes Beginning Java and the fly likes Can you put an object in as a Hashmap value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you put an object in as a Hashmap value?" Watch "Can you put an object in as a Hashmap value?" New topic
Author

Can you put an object in as a Hashmap value?

Amy Richards
Greenhorn

Joined: Jun 17, 2010
Posts: 2
Hi Everyone, I'm new! I'm in a Java class at work and really liking it. I'm stuck on a problem. The assignment was regarding hashmaps. The way I implemented it was by puting a seriel number in as the String key and then objects of type "Thing" for the Value. Can objects be used in a hashmap. Clearcase allowed me to do this, but now I can't seem to find any examples of code where others are using objects in hashmaps. I want to use a method from Thing on the iterator and that's where i'm stuc and so I was hoping to find an example. Does anyone have any examples? Should I post my code in here?

THANKS!
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Welcome to the Java Ranch!

You really can and should post code examples if you have questions about those (although if you're really new, you might want to post in the beginners forum ;)

As to HashMaps, yes you can put any types of objects in them as values. To obtain objects from the Map, you typically use the get() method of the Map, or access the values() of the Map with an Iterator (or better, in a for loop). It looks like this:



Have a look at the API for the HashMap class...it will show you all the details you need
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In fact, you *must* put objects in as a hashmap value. And key.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
{picky-mode}Always remember you are storing a reference to an object not the object itself. Talking about programming concepts correctly helps you avoid misteaks - references to that object may exist nowhere else or 1000 places. {/picky-mode}

Bill
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Let's slide this question over to the Beginning Java forum, where I think this question fits better.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Amy Richards
Greenhorn

Joined: Jun 17, 2010
Posts: 2
Thanks! This was really helpful! You can move it over to the beginning Java, that's great. Also, now that it's resolved, do I need to close it or how do the moderators do that?

Thanks again!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Amy Richards wrote:Also, now that it's resolved, do I need to close it or how do the moderators do that?


If you feel that your topic has been resolved, then you can click the big brown "resolve" button on the bottom -- and someone can check and confirm whether this is true.

However... since you asked a moderator to do it -- I'll confirm it for it. And hence, the big button will be gone.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you put an object in as a Hashmap value?