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

What are the concepts of 'OOPS'?

0 Answers   Atos Origin,


Marker interface means , interface which has no methods.Then what is the necessity of its usage.I read "it tells the compiler that it should be treated differently ". "It is used to store state of an object". But still am not clear.Please explain clearly.

2 Answers  


Why runnable interface is used in java?

0 Answers  


What is finalize()? Is finalize() similar to a destructor?

0 Answers  


What is singleton class?

16 Answers   3i Infotech, 7 Seas, ABC, Amdocs, Cap Gemini, Oracle, Persistent, TCS, Techforza,






What is meant by throwing an Exception?

4 Answers   Accenture,


When do you call copy constructor?

0 Answers   Tavant Technologies, Virtusa,


why HashTable not allow null key and value

2 Answers   Crimson Logic,


how we can create packages in java?

0 Answers  


What is the vector class in java programming?

0 Answers  


What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. Import java.net.* Versus import java.net.socket)?

0 Answers  


What is the purpose of final keyword and when to use it?

0 Answers  


Categories