Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

hashtables

Thread view: 
Tuurbo46 - 31 Oct 2005 22:29 GMT
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
Oliver Wong - 31 Oct 2005 22:40 GMT
> 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
Chris Smith - 31 Oct 2005 23:57 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 01 Nov 2005 07:34 GMT
>1) Is it true we cannot put integers or characters straight into a
>hashtable.

see http://mindprod.com/jgloss/hashtable.html
http://mindprod.com/jgloss/hashmap.html
http://mindprod.com/jgloss/hashcode.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.