Regarding Hash map and Hastable

HI,Can any one, pls tell me when to use Hashtable and Haspmap.regards,Balu
[102 byte] By [BaluChakrapania] at [2007-11-27 5:27:48]
# 1

Hashmap and Hashtable both accept key-value pairs with a prime difference, i.e. Hashmap permits null values where as hash table doesn't.

Another difference being while iterating over the hashMap, if another object tries to modify the structure of hashmap object, then it throws a ConcurrentModificationException. Hashtable is synchronized and only one thread can modify it at any point of time.

SirG

SirGenerala at 2007-7-12 14:49:40 > top of Java-index,Core,Core APIs...
# 2
A Hashtable can throw that exception too while iterating. Its methods are synchronized but that can't solve that problem.
ejpa at 2007-7-12 14:49:40 > top of Java-index,Core,Core APIs...
# 3

The javadoc for HashMap says:

"...(The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) ..."

When to use:

if you need synchronization (Multithreaded app) prefer to use Hashtable [or Collections.synchronizedMap(new HashMap(...))], else the HashMap (probably a little bit faster).

[]

S_i_m_ua at 2007-7-12 14:49:40 > top of Java-index,Core,Core APIs...
# 4

My guidelines (similar for Vector)

Use Hashtable only when forced to by an interface you are using (eg as a parameter to the constructor in javax.naming.InitialContext: new InitialContext(Hashtable environment)

Otherwise use HashMap. If you require synchronization, use Collections.synchronizedMap.

evnafetsa at 2007-7-12 14:49:40 > top of Java-index,Core,Core APIs...