• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

HashMap vs Hashtable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I googled around for the differences between these and most of the websites stated the following -
"The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.
Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.
And, a third difference is that HashMap permits null values in it, while Hashtable doesn't. "

I am unable to understand the second difference (that the hashmap iterator is failsafe). The hashmap doesnt have a iterator in the first place and the only way to get a iterator would be something like Iterator<Map.Entry<String, Integer>> it = g.entrySet().iterator(); Does that mean that while accessing the iterator i cannot add elements to the hashmap ? because I tried that and the program worked without giving me an error. Can anyone explain what the second difference actually means? Also does the second difference pose a benefit since hashtable is synchronized and the only possible way in which the contents can be modified in a different thread is if that thread can obtain a lock on the object.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fail safe meaning in thread context while accessing mutiple threads


The iterators for the concrete collection implementations
are fail-fast.
That means that if you are using an Iterator to traverse a
collection while underlying collection is being modified by
another thread, then the Iterator fails immediately by
throwing a ConcurrentModificationException (another
RuntimeException). That means the next time an Iterator
method is called, and the underlying collection has been
modified, the ConcurrentModificationException exception gets
thrown.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me put in easy words:

While iterating through iterator a hashmap can be changed but at the same time if any other thread modifies the same object at the time of iteration through iterator, iterator will throw an exception
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A ConcurrentModificationException will be thrown

ConcurrentModificationException
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manish R Singh wrote:That means that if you are using an Iterator to traverse a
collection while underlying collection is being modified by
another thread, then the Iterator fails immediately by
throwing a ConcurrentModificationException...



In fact the failure will occur even if it's the same thread which modifies the underlying collection. Many people are surprised when this happens, perhaps because they have seen explanations like this one.
 
sandeep raj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the explanation guys.
@paul - I actually tried modifying the hashmap after obtaining the iterator and wasnt posed with any exception. what i did was basically
get the iterator using Iterator<Map.Entry<String, Integer>> it = g.entrySet().iterator(); I then looped on the iterator 'i' and inserted additional values in the middle of the loop. The iterator did not print out the newly inserted values and it did not give me an exception, so I am wondering why i wasnt posed with an exception based on what you said. All the code of obtaining the iterator and looping was within the main thread.
 
Marshal
Posts: 80667
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would appear you have successfully added values, but you have only added values to the Set which the entrySet method returns. You haven't added anything to the original Map. Maybe you have entered the values before the position of the Iterator, so you would never find them.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It would appear you have successfully added values, but you have only added values to the Set which the entrySet method returns. You haven't added anything to the original Map.


Actually, entrySet() returns a view, so removing anything from it should remove the underlying map entry as well. As far as I know, adding elements to keySet(), values() or entrySet() will throw an UnsupportedOperationException for all known Map implementations.
 
Campbell Ritchie
Marshal
Posts: 80667
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Rob, you are right.

There must be a different explanation. Can't think what it is at the moment. An Iterator doesn't have an add method, so you shouldn't be able to add with the Iterator, only remove elements.
 
reply
    Bookmark Topic Watch Topic
  • New Topic