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. Thanks.
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. Jason
Joined: Jul 11, 2000
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. R.
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... Joel
Wait a minute, I'm trying to think of something clever to say...<p>Joel
Chicken Farmer ()
Joined: May 08, 2001
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: Boolean Byte Character Short Integer Long Float Double 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. Jason