java - Clarification @ConcurrentModificationException in HashMap -
hi expecting concurrentmodificationexception in below secnarion,but it's working fine.
could please clarify these one...
hashmap<integer, string>table1 = new hashmap<integer, string>(); table1.put(1, "sam"); table1.put(2, "jon"); table1.put(3, "doe"); iterator itr1 = table1.entryset().iterator(); table1.put(3, "donn"); while(itr1.hasnext()) { system.out.println("---value--" + itr1.next()); }
as per javadoc hashmap
the iterators returned of class's "collection view methods" fail-fast: if map structurally modified @ time after iterator created, in way except through iterator's own remove method, iterator throw concurrentmodificationexception.
so since modifying hashmap
after getting iterator
should getting concurrentmodificationexception
putting entry existing key not considered structural modification in current implementation of hashmap
and never trigger concurrentmodificationexception
. try putting new key, e.g. table1.put(4, "ups");
concurrentmodificationexception
.
Comments
Post a Comment