11

Say I am iterating over a Map in Java... I am unclear about what I can to that Map while in the process of iterating over it. I guess I am mostly confused by this warning in the Javadoc for the Iterator interface remove method:

[...] The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

I know for sure that I can invoke the remove method without any issues. But while iterating over the Map collection, can I:

  1. Change the value associated with a key with the Map class put method (put with an existing key)?

  2. Add a new entry with the Map class put method (put with a new key)?

  3. Remove an entry with the Map class remove method?

My guess is that I can probably safely do #1 (put to an existing key) but not safely do #2 or #3.

Thanks in advance for any clarification on this.

4 Answers 4

13

You can use Iterator.remove(), and if using an entrySet iterator (of Map.Entry's) you can use Map.Entry.setValue(). Anything else and all bets are off - you should not change the map directly, and some maps will not permit either or both of the aforementioned methods.

Specifically, your (1), (2) and (3) are not permitted.

You might get away with setting an existing key's value through the Map object, but the Set.iterator() documentation specifically precludes that and it will be implementation specific:

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. (emphasis added)

2
  • 1
    I confirm that your suggestions are nice. Map.Entry.setValue() works like a charm, Iterator.remove() too.
    – gouessej
    Commented May 3, 2012 at 22:09
  • For completeness, what about modifying the iterator current value via public mutator? There's no way the Map has visibility into every entry's method calls, right?
    – Patrick M
    Commented Oct 8, 2015 at 16:14
2

If you take a look at the HashMap class, you'll see a field called 'modCount'. This is how the map knows when it's been modified during iteration. Any method that increments modCount when you're iterating will cause it to throw a ConcurrentModificationException.

That said, you CAN put a value into a map if the key already exists, effectively updating the entry with the new value:

 Map<String, Object> test = new HashMap<String, Object>();
 test.put("test", 1);

 for(String key : test.keySet())
 {
     test.put(key, 2); // this works!
 }

 System.out.println(test); // will print "test->2"

When you ask if you can perform these operations 'safely,' you don't have to worry too much because HashMap is designed to throw that ConcurrentModificationException as soon as it runs into a problem like this. These operations will fail fast; they won't leave the map in a bad state.

1
  • These methods try to fail-fast, but due to memory concurrency issues they may not always if the modification is done in another thread. Always program to the API documentation; never violate it because "I tried it and it worked", since it might work only on your (a) JVM, or (b) hardware, or (c) specific configuration but not others. Commented Jan 9, 2014 at 23:55
2

There is no global answer. The map interface let the choice to the users. Unfortunately, I think that all the implementations in the jdk use the fail-fast implementation (here is the definition of fail-fast, as it stated in the HashMap Javadoc):

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. 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.

0

In general, if you want to change the Map while iterating over it, you should use one of the iterator's methods. I have not actually tested to see if #1 will will work, but the others definitely will not.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.