Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hashmap and Hashtable

Posted on 2011-09-07
6
Medium Priority
?
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:rumi78
rumi78 earned 800 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 1200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

604 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