Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Hashmap and Hashtable

I would like to know about Hashmap and Hashtable.

I
 read iterator in  Hashmap is fail safe where as Hashtable it is not. I was not clear on that. When, how, why we use each of them, advantages, disadvantages of them. thanks in advance
Avatar of for_yan
for_yan
Flag of United States of America image

Avatar of rumi78
rumi78

It is from soruce code:
   82    * <p>The iterators returned by the <tt>iterator</tt> method of the collections
   83    * returned by all of this class's "collection view methods" are
   84    * <em>fail-fast</em>: if the Hashtable is structurally modified at any time
   85    * after the iterator is created, in any way except through the iterator's own
   86    * <tt>remove</tt> method, the iterator will throw a {@link
   87    * ConcurrentModificationException}.  Thus, in the face of concurrent
   88    * modification, the iterator fails quickly and cleanly, rather than risking
   89    * arbitrary, non-deterministic behavior at an undetermined time in the future.
   90    * The Enumerations returned by Hashtable's keys and elements methods are
   91    * <em>not</em> fail-fast.
   92    *
   93    * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
   94    * as it is, generally speaking, impossible to make any hard guarantees in the
   95    * presence of unsynchronized concurrent modification.  Fail-fast iterators
   96    * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
   97    * Therefore, it would be wrong to write a program that depended on this
   98    * exception for its correctness: <i>the fail-fast behavior of iterators
   99    * should be used only to detect bugs.</i>

Difference between hashtable and hashmap: hashmap is not thread save, hashtable is thread save. it means that putting vales to map in concurrent threads may cause undeterministic result on hashmap, but will work on hashtable. hashmap is faster (it does not use monitors to controll concurrent access)

however it is not related to the concurrentmodificationexception.  
when you see concurrentmodifcationexception you have to change the code - you can not releayed on it (like mentioned above)
Avatar of gudii9

ASKER

>>>Difference between hashtable and hashmap: hashmap is not thread save, hashtable is thread save. it means that putting vales to map in concurrent threads may cause undeterministic result on hashmap, but will work on hashtable.

that means HashMap iterator should not be failsafe where as the Hashtable is failsafe right??

 which is kind of opposite to what i read initially. please advise
SOLUTION
Avatar of rumi78
rumi78

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

i am still confused. can you please elaborate on this
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial