• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap vs Hashtable  RSS feed

 
sandeep raj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
Manish R Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • 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
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A ConcurrentModificationException will be thrown

ConcurrentModificationException
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 56541
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!