Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you put an object in as a Hashmap value?

 
Amy Richards
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, you *must* put objects in as a hashmap value. And key.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's slide this question over to the Beginning Java forum, where I think this question fits better.
 
Amy Richards
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic