wood burning stoves 2.0*
The moose likes Cattle Drive and the fly likes Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Hashmap" Watch "Hashmap" New topic


Ronald Schindler
Ranch Hand

Joined: Jul 11, 2000
Posts: 50
I've begun work on OOP-2. I must admit, I knew nothing about hashmaps when I started. Funny, I still feel that way. However, I was able to follow some good threads from previous questions here.
The one thing I didn't find though was: How does one know when a Hashmap should be used? I am assuming that there must be some general rules that one can apply to know when to use a Hashmap as opposed to coding the solution another way.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I use a HashMap anytime I need a collection where a name, id, or something along those lines associates with an object. Anytime it makes sense for a key to be used to refer to value. If I just need a grouping of things, but am not going to be calling specific objects based on an identifier (not to be confused with a variable identifier), I just use an ArrayList or something like that.
Good example, I have a group of students in a collection. If I want to call a student object based on a name, I would use a HashMap. If I just want to iterate through the students to calculate grades, I would just use a list of some sort.
Not sure if that is the "best" reasoning, but it's the way I do it.
Ronald Schindler
Ranch Hand

Joined: Jul 11, 2000
Posts: 50
Okay, I'm still struggling with using a HashMap for OOP2.
I reviewed the api for HashMap. I know that after instantiation the Hashmap will take an key and a value and store them. Where, I'm not exactly sure just yet. This action is accomplished through -- put(Object key, Object value) -- which returns a value.
As I now look at this method, my confusion is this: I expected to store a number and an identifier by which to call it. However, HashMap requires that I use a type of Object. I don't understand exactly what it is asking for or even why it is needed.
Any assistance and/or further documentation on using this class would be appreciated.
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
I just went through the same problems doing OOP-2, but after a bit of frustration I figured it out, so let's analyze what you are trying to do...
A Hashmap has two parts, the key and the value. As you pointed out, both of these are of type "Object".
The key is to understand that almost everything is an Object, but there are two things to remember which should get you through this...
1) An Object must be instantiated.
2) Some things are not Objects, they are primitives...
The answer lies in how to treat a primitive like an Object.
HTH, I'm trying not to give too much away...

I'm a soldier in the NetScape Wars...

Wait a minute, I'm trying to think of something clever to say...<p>Joel
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Are you familiar with the Wrapper classes? Each primitive data type has a wrapper class that encapsulates that primitive type in an object. They are:
Look through the API at these classes and their methods. They are EXTREMELY useful and it is important you understand how to use them.
Also, when doing the put( key , value ), you do have to instantiate, but you can do that without an identifier, like put( new Object() , new Object() ); Probably knew that, but some people get hung up on that.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9044
Strings are also Objects

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
They are!?!?!
I agree. Here's the link: http://aspose.com/file-tools
subject: Hashmap
Similar Threads
HashMap access problem
How to use a HashMap
Locking problem
HashMap doubt . Thanks for comments.
help,about HashMap!