Solved

Hashmap and Hashtable

Posted on 2011-09-07
6
365 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

932 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

10 Experts available now in Live!

Get 1:1 Help Now