Solved

Hashmap and Hashtable

Posted on 2011-09-07
6
364 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:gudii9
  • 2
  • 2
  • 2
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36495737
0
 
LVL 7

Expert Comment

by:rumi78
ID: 36498318
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)
0
 
LVL 7

Author Comment

by:gudii9
ID: 36517668
>>>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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Assisted Solution

by:rumi78
rumi78 earned 200 total points
ID: 36520087
I dont know what you read as "failsafe".
Ive only found "rumors" about Concurrent modification exception throwing as "failsafe" reaction.

If you access your map (regardless hashmap or hashtable) in single thread, concurrent modification exception is always thrown when you modify the map during iteratate, e.g.

for(Iterator i=map.iterator();i.hasNext();){
     map.remove("xx"); // or map.put("xx","yy");
}

0
 
LVL 7

Author Comment

by:gudii9
ID: 36642085
i am still confused. can you please elaborate on this
0
 
LVL 47

Accepted Solution

by:
for_yan earned 300 total points
ID: 36653749

Read below - thsi is what menas that Hashmap is not synchronized - if you writing multithreaded application and
everal thread are accessing and mdofying - adding or deleting entries and may be doing it simmulatneously -
it is responsibility of the programmer to ensure that when one thread
accesses hashtable another should be blocked form doing it

In case of Hashtable - programmer may not worry about it as Hastable will not allow two threads to
modify it simulatneously. So in case of multithread it is safer , but because of synchronization Hashatble is
slower
 
This is HashMap from
http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

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

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 47
parentbit challenge 3 52
Free Alternative to JIRA 4 55
GUI builder for Eclipse? 8 10
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now