3

I am expecting a ConcurrentModificationException in the follow code, but it's working fine.

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 the JavaDoc for 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 method, the iterator will throw a ConcurrentModificationException.

So since I am modifying the HashMap after getting the Iterator I should be getting the ConcurrentModificationException. Why is it not throwing?

1
  • 1
    The documentation also states: "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." Commented Mar 6, 2014 at 11:59

3 Answers 3

3

Putting an entry for an existing key is not considered a structural modification in the current implementation of HashMapand will never trigger a ConcurrentModificationException. Try putting with a new key, e.g. table1.put(4, "UPS"); to get a ConcurrentModificationException.

Sign up to request clarification or add additional context in comments.

Comments

2

You are modifying the HashMap not the entrySet that you are obtaining on it, and here you are obtaining the iterator over the entrySet:

As per entrySet method javaDoc:

If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined.

So you are not getting ConcurrentModificationException.

Here is the full explaination of entrySet:

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

2 Comments

“undefined” does not preclude throwing a ConcurrentModificationException and in the current implementation it does throw a ConcurrentModificationException but only if keys are added or removed.
Yup, got it, since he is using the same key, the HashMap is not getting modified.
2

try table1.put(4, "DONN") and iterator will fail with ConcurrentModificationException. table1.put(3, "DONN") does not change map structure but just replaces value for key=3 since it's already there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.