?
Solved

Hashmap and Hashtable

Posted on 2011-09-07
6
Medium Priority
?
390 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

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!

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

770 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