Which One is optimal to choose ?

Syncronized hash map or Hash table with single thread model?

How can a hash map syncronized with out using syncrozed
blocks in programm?

Answers were Sorted based on User's Feedback



Which One is optimal to choose ? Syncronized hash map or Hash table with single thread model? ..

Answer / ranganathkini

The java.util.Hashtable provides a synchronized collection
implementation to store key-value pairs. Hence
synchronization of a Hashtable is not necessary.

Synchronization has a performance overhead and therfore must
be used with caution in only those situations where a
resource(s) is shared by multiple threads. Hence a
java.util.HashMap implementation can be used for single
threaded model and it can be externally synchronized if it
is to be used in a mult-threaded model.

To get a synchronized version of a java.util.HashMap without
using synchronized blocks, do this:

// make sure this is in imports
import java.util.*;

// our unsynchronized map object
Map myMap = new HashMap();

// get a synchronized map from our unsynchronized map
Map mySyncMap = Collections.synchronizedMap( myMap );

After this, all access to the map must be via mySyncMap
reference and NOT myMap.

Another way to acquire a synchronized map is:

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

Is This Answer Correct ?    5 Yes 1 No

Which One is optimal to choose ? Syncronized hash map or Hash table with single thread model? ..

Answer / ravikiran

Collections.synchronizedMap()

Is This Answer Correct ?    2 Yes 1 No

Which One is optimal to choose ? Syncronized hash map or Hash table with single thread model? ..

Answer / dsr

Collections.synchronizedMap(HashMap object)

Is This Answer Correct ?    1 Yes 1 No

Post New Answer

More Core Java Interview Questions

How will you communicate between two applets?

0 Answers  


What do you mean by object?

0 Answers  


What is the difference between Trusted and Untrusted Applet ?

2 Answers   IBM,


who can we create the object of a class? in how many ways we can create it (max 5)

2 Answers  


Are true and false keywords?

0 Answers  


What is int lol?

0 Answers  


why are wait(), notify() and notifyall() methods defined in the object class? : Java thread

0 Answers  


Explain java coding standards for variables ?

0 Answers  


How do you compare values in java?

0 Answers  


What is the byte order of byte buffer?

0 Answers  


Why java applets are more useful for intranets as compared to internet?

0 Answers  


What does it mean that strings are immutable?

0 Answers  


Categories