Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Iterators on HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Iterators on HashMap" Watch "Iterators on HashMap" New topic
Author

Iterators on HashMap

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
While reading about difference of HashMap and HashTable I came across following statement.


Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't.


Fail-safe is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally", a concurrent modification exception will be thrown. It is possible for other threads though to invoke "set" method since it doesn't modify the collection "structurally". However, if prior to calling "set", the collection has been modified structurally, "IllegalArgumentException" will be thrown.

I could not understand that why should the exception be throw only when there is structural modification.

Can some one please give example.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Naresh Chaurasia wrote:I could not understand that why should the exception be throw only when there is structural modification.

adding an element into a map/list when you iterate that doesnt make sense. elements are added to the list at the time of iterating the list which keeps the list grow, never end.
if it could not throw ConcurrentModificationException[fail-fast], it throw an OutOfMemoryException after many Iteration and operations which waste your and cpu time.

note:enumeration is not a fail-fast mechanism. so using an iterator or for-each is a good practice.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
If I understand this correctly, error is thrown if the number of elements in the map changes (you either add or remove elements). It makes sense, because iterator coul then return an element that no longer exists in map or skip an element while passing trough the Hashmap.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Naresh,

see if this info might help (from the JavaDocs)

...from Class HashMap

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.


...and Interface Map


put
public Object put(Object key, Object value)
Throws:
IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.


"Eppur si muove!"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Iterators on HashMap
 
Similar Threads
Confusion in ConcurrentModificationException
Collection Error
Will there be any senario where Hashtable and Vector need special code for thread safety
Setting Fields value through JNI
Threadsafe iteration over non-indexed collection?