Synchronise Hashmap

I got a ClassCast Exception for this..How to correct it?
Hashmap m = (HashMap)Collections.synchronizedMap(new HashMap(...));

Basically I need a synchronise Hashmap.
Or anyone else can suggest a type that can perform better then Hashmap and provide synchronisation..
LVL 9
william007Asked:
Who is Participating?
 
objectsCommented:
Its a Map., not a HashMap. there is no such thing as a synchonized Hashmap.
use:

Map m = Collections.synchronizedMap(new HashMap(...));

anotyher option would be to use a Hashtable. It is synchronised.
0
 
william007Author Commented:
For Map and HashTable, may I know which performance is better?
0
 
sriki77Commented:
HashMap is faster than Hashtable, because of lack of sychronization.

The synchronized Map and Hashtable performance can be comparable. No clear winner here.

As suggested use Hashtable when you need sychronized option always. Use the sychnonized Map when you want both sychonization from some access and no synchronization for certain access
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
> For Map and HashTable, may I know which performance is better?

Hashtable is most likely faster than a synchnozied wrapper around  a HashMap.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, it is, just like we told you in your other Q that a Vector is faster than a synchronized wrapper over an array-list.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
william007Author Commented:
Thanks:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.