Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-663

Unexpected ConcurrentModificationException when altering Collection of a MultiValuedMap

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Trivial
    • Resolution: Unresolved
    • None
    • None
    • None
    • None

    Description

      Testcase:

      	@Test
      	public void test() {
      		MultiValuedMap<Integer, Integer> multiMap = new HashSetValuedHashMap<>();
      		multiMap.put(1, 10);
      		multiMap.put(2, 20);
      		for (Collection<Integer> innerCollection : multiMap.asMap().values()) {
      			for (Iterator<Integer> iterator = innerCollection.iterator(); iterator.hasNext();) {
      				Integer i = iterator.next();
      				iterator.remove(); // only the innerCollection is altered
      			}
      			// innerCollection.add(6); // adding stuff back should also work...
      		}
      	}

      This test unexpectedly throws a ConcurrentModificationException.
      The issue is that when calling iterator.remove() the AbstractMultiValuedMap.ValuesIterator detects that the Collection is empty and calls AbstractMultiValuedMap.this.remove(key);.

      It may be better if the iterator of the inner collection had a reference on the iterator if the outer map and called containerIterator.remove() instead.
      Note: this solution would again present issues if the user tries to add new elements in this now empty collection (which was removed from the parent).

      In the current state, it is quite unclear why an exception is thrown, without debugging the code.

      Attachments

        Issue Links

          Activity

            People

              kinow Bruno P. Kinoshita
              chris333 Christophe Schmaltz
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 0.5h
                  0.5h