Re: Filtering to-many relationship and ConcurrentModificationException

From: Bryan Lewis (brya..aine.rr.com)
Date: Tue Sep 13 2005 - 09:36:49 EDT

  • Next message: Joshua Pyle: "Re: Filtering to-many relationship and ConcurrentModificationException"

    Here's another way that worked for me.

                // Use a ListIterator to allow concurrent modification.
                for (ListIterator it = getRoles().listIterator(); it.hasNext(); ) {
                    Role role = (Role) it.next();
                    // Remove it from the iterator too, to avoid a
                    // ConcurrentModificationException.
                    it.remove();
                    removeFromRoles(role);
                }

      ----- Original Message -----
      From: Jeff de Vries
      To: cayenne-use..bjectstyle.org
      Sent: Monday, September 12, 2005 11:33 PM
      Subject: Filtering to-many relationship and ConcurrentModificationException

      I'm trying to iterate over a to-many relationship filtering out unwanted objects, something like this:

        Iterator it = getFooArray().iterator();
        while (it.hasNext()) {
           Foo f = (Foo) it.next();
           if some condition on f is true {
              removeFromFooArray(f);
           }
        }

      At runtime, I'm getting a ConcurrentModificationException, which can be raised either in a multithreading situation (which I don't think I'm in), or by "invalidating" the contract of the container (i.e. while in a loop deleting items all of sudden adding an item).

      Does removeFromXXXArray() invalidate any iterators iterating over the list? Is there a better way to do what I'm trying to do? Apologies in advance if this is just a dumb Java question.

      Thanks,
      Jeff de Vries



    This archive was generated by hypermail 2.0.0 : Tue Sep 13 2005 - 09:35:52 EDT