| Author |
Hashtable doent allow null values ???
|
Naveen kumar.s
Greenhorn
Joined: Feb 10, 2010
Posts: 3
|
|
Many tutorials say that Hashtable doesnt allow null values,but i tried like
hashtable.put(" ","red");
and able to display it by using Object get(Object key);
and also i want to know whether Hashtable allow duplicates.
I think the only diff b/w HashMap and Hashtable is Hashtable is synchronized.
Am i right?
|
 |
Pushkar Choudhary
Rancher
Joined: May 21, 2006
Posts: 425
|
|
Naveen kumar.s wrote:Many tutorials say that Hashtable doesnt allow null values,but i tried like
hashtable.put(" ","red");
and able to display it by using Object get(Object key);
" " is not a null value, it is a valid String with a single space. "" is also a valid empty String. Try putting null.
|
 |
PrasannaKumar Sathiyanantham
Ranch Hand
Joined: Nov 12, 2009
Posts: 110
|
|
|
"" means an empty string or a string with length 0 . try using null you will get an error
|
To err is human,
To forgive is not company policy
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Welcome to JR !
The " " is empty string and NOT a null value. In Java, null is object, like
And hash is a key so it must be unique in order to retrieve the corresponding value.
Have look at the JavaDocs for the Hashtable for more info.
And yes, Hashtable is synchronized.
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
|
and people are very quick here ;)
|
 |
Naveen kumar.s
Greenhorn
Joined: Feb 10, 2010
Posts: 3
|
|
Hi,
thank you all for your replies,
but i still have doubts regarding null values in hash table.
i have entered null as key and when i use get("null"), the value that is paired to null key is being displayed.
for your reference am giving the code and output here.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Naveen kumar.s wrote:
i have entered null as key and when i use get("null"), the value that is paired to null key is being displayed.
null --> meaning null reference. Not a string with a value of "null".
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
shan raj
Ranch Hand
Joined: Dec 16, 2008
Posts: 42
|
|
Hi,
Look at the previous response for the thread.
Here also you are using valid string "null" with length 4 as key used for storing the value and retrieving the value.
In your code try this
instead of
Thanks
shan
|
 |
Naveen kumar.s
Greenhorn
Joined: Feb 10, 2010
Posts: 3
|
|
Thanks, yes that works well when i enter null instead of "null" as a key.
thank you all for your responses.
|
 |
 |
|
|
subject: Hashtable doent allow null values ???
|
|
|