• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashtable doent allow null values ???

 
Naveen kumar.s
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"" means an empty string or a string with length 0 . try using null you will get an error
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and people are very quick here ;)
 
Naveen kumar.s
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shan raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, yes that works well when i enter null instead of "null" as a key.

thank you all for your responses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic