Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Difference between HashTable and Collections.synchornizedMap(new HashMap())

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

This question might be asked before. What is the difference between Hashtable and Collections.synchronizedMap(new HashMap()) ???

I searched the net also but not able to understand any substantial difference for my knowledge. Please do point me to some link which might help.

Kindly please help me..

Thanks
Sid.
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did a quick look through the API documentation and I see that HashMap allows null keys and null values whereas Hashtable doesn't. Hashtable has an elements() method which returns an Enumeration whereas HashMap doesn't. There could be plenty of other differences, I invite you to look at the docs for yourself.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I just did a quick look through the API documentation and I see that HashMap allows null keys and null values whereas Hashtable doesn't. Hashtable has an elements() method which returns an Enumeration whereas HashMap doesn't. There could be plenty of other differences, I invite you to look at the docs for yourself.



Please Note I am asking the difference between Hashtable and Collections.synchronizedMap(new HashMap()) and not Hashtable and HashMap. I already know the difference between Hashtable and HashMap.
 
Bartender
Posts: 2230
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashtable has its method synchronized. Like:Collections.synchronizedMap wraps the collection using synchronized methods. Like:
With regards to thread safety, they both do the same thing.

There is also ConcurrentHashMap but it has a different thread-0sadety policy, Did you have a chance to check it?

code samples from JDK 1.8.0_40
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Hashtable has its method synchronized. Like:Collections.synchronizedMap wraps the collection using synchronized methods. Like:
With regards to thread safety, they both do the same thing.

There is also ConcurrentHashMap but it has a different thread-0sadety policy, Did you have a chance to check it?

code samples from JDK 1.8.0_40



Thanks for your reply. Yes I do know about ConcurrentHashMap. So if anyone asks me about the difference between Hashtable and Collections. syncronizedMap(new HashMap()) then would the above explanation be sufficient ?? are there any more differences which I cant let them know. ?? So basically what we are saying is that Hashtable synchronizes on the method level and Collections.synhronizedMap(new HashMap()) synchronizes at the object (mutex) level. rt. ???
 
Rancher
Posts: 4625
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answer would be that Hashtable is pre-Collections and, like Vector, should be viewed as legacy and, consequently, not likely to be as well maintained.
 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Siddharth Bhargava wrote:

Paul Clapham wrote:I just did a quick look through the API documentation and I see that HashMap allows null keys and null values whereas Hashtable doesn't. Hashtable has an elements() method which returns an Enumeration whereas HashMap doesn't. There could be plenty of other differences, I invite you to look at the docs for yourself.



Please Note I am asking the difference between Hashtable and Collections.synchronizedMap(new HashMap()) and not Hashtable and HashMap. I already know the difference between Hashtable and HashMap.



Okay. Then whatever I said about HashMap in my original post also applies to Collections.synchronizedMap(new HashMap()). It was just too tedious to type the latter's name all the time.
 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Siddharth Bhargava wrote:Thanks for your reply. Yes I do know about ConcurrentHashMap. So if anyone asks me about the difference between Hashtable and Collections. syncronizedMap(new HashMap()) then would the above explanation be sufficient ?? are there any more differences which I cant let them know. ?? So basically what we are saying is that Hashtable synchronizes on the method level and Collections.synhronizedMap(new HashMap()) synchronizes at the object (mutex) level. rt. ???



That explanation referred specifically to code in one Oracle JVM. You shouldn't assume that it's true about other JVMs unless those differences are specified in the API documentation.
 
    Bookmark Topic Watch Topic
  • New Topic