• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashtable doent allow null values ???

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"" means an empty string or a string with length 0 . try using null you will get an error
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and people are very quick here ;)
 
Naveen kumar.s
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic