[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

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..
0
william007
Asked:
william007
  • 2
  • 2
  • 2
  • +1
4 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now