Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections.synchronizedMap or thread safe classes in collection which one is better?

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map myMap = Collections.synchronizedMap (myMap);
synchronizes the operations on myMap collection class my question is if i have synchronize any way then why i should not be using Hashtable ? is Collections.synchronizedMap provide some extra functionality
 
cherry ch
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think HashTable and Vector is not a good idea
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have also been told that Hashtable and Vector make your Operation slow and but would not Collections.synchronizedMap be making your operations on map slow? my question is more towards what is the advantages developers see in using Collections.synchronizedMap than to use Hashtable.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanu Gulati wrote: my question is more towards what is the advantages developers see in using Collections.synchronizedMap than to use Hashtable.

using Collections.synchronizedMap you may very quick and easy make any map thread save.
Any map - even a map returned from old libraries you use, but you don't have access to their source code.
 
Rok Ć telcer
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Vector & Hashtable are both leftovers (legacy) from 1.0 API and still available only due to the compatibility reasons ...
cherry ch wrote:
I have also been told that Hashtable and Vector make your Operation slow and but would not Collections.synchronizedMap be making your operations on map slow?

In general the above statement is true (twice), however you're missing the point.
The idea is, that we have the control whether we want too synchronize our collection or not.
Beside this, a Map has various specialized implementations designed for different cases and/or usages (in regards the performance): access, removal, inserting, "ordering", etc..

Hope this helps.
If not, please read the following link:
http://java.sun.com/docs/books/tutorial/collections/index.html


Regards,
Rok
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic