gudii9
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
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
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 * ConcurrentModificationExce ption}. 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>ConcurrentModification Exception< /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 concurrentmodificationexce ption.
when you see concurrentmodifcationexcep tion you have to change the code - you can not releayed on it (like mentioned above)
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 * ConcurrentModificationExce
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>ConcurrentModification
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 concurrentmodificationexce
when you see concurrentmodifcationexcep
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i am still confused. can you please elaborate on this
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
lots of explanations here:
http://stackoverflow.com/questions/40471/java-hashmap-vs-hashtable