hashtables

T

Tuurbo46

Hi

I currently need a bit of advice.

1) Is it true we cannot put integers or characters straight into a
hashtable. The intergers or characters have to be first put into an
object-wrapper and then put into an hashtable?

2) To put an object into a hashtable we use e.g. hT.put("harry"); and to
remove we have to use hT.keys. Why dont we have to use keys to put objects
into a hashtable?

3) I dont really understand the keys part. Please could somebody help.

Thanks Tuurbo
 
O

Oliver Wong

Tuurbo46 said:
1) Is it true we cannot put integers or characters straight into a
hashtable. The intergers or characters have to be first put into an
object-wrapper and then put into an hashtable?

True, except Java 1.5 will sometimes automatically create the
object-wrapper for you (it's called Autoboxing). So syntactically, it may
look like you are putting integers in the hashtable, but under the hood,
you're actually secretly creating the object-wrapper and putting that in the
table.
2) To put an object into a hashtable we use e.g. hT.put("harry"); and to
remove we have to use hT.keys. Why dont we have to use keys to put
objects into a hashtable?

False. You DO need to use keys to put objects into a hashtable. The
proper syntax is:

ht.put("key", "value");
ht.get("key");
3) I dont really understand the keys part. Please could somebody help.

Are you familiar with arrays? Think of Hashtables as a special kind of
Array. With arrays, the keys are integers in a specific range. So in the
statement:

myArray[4] = "Hello!"

4 is the key and "Hello!" is the value. Hashtables are like that except
there are almost no restriction on the keys. That is, the keys can be any
object; they don't have to be Integers. When you want to put something into
the hashtable, you have to specify where you want to put the value, and you
have to specify the value itself. when you want to get something out of a
hashtable, you just tell it where to look for the value and it'll give you
the value.

Another (perhaps slightly "wrong", but more intuitive) way to look at it
is to use key as a metaphor for actually locking the object away. You put
the object in the hashtable and then use a key to lock it away. You need to
hang onto the key. Later on, when you want the object back, you have to use
the same key to get the object back. There are a few technical issues with
this analogy (mainly with the "sameness" of keys, and the .equalsTo()
method), so if the first "array-like" explanation made sense to you, use
that one instead of this one.

- Oliver
 
C

Chris Smith

Tuurbo46 said:
2) To put an object into a hashtable we use e.g. hT.put("harry"); and to
remove we have to use hT.keys. Why dont we have to use keys to put objects
into a hashtable?

I interpreted this question differently from Oliver, so here's my
response.

You do NOT need to call hT.keys() method to get things out of a
Hashtable. Instead, you can just call hT.get(...) and pass the key.
It's only if you've forgotten the key that you need to use hT.keys() (or
some other method) to find it. When you already have the key, you don't
need to use hT.keys() at all, regardless of whether you're putting or
getting.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top