I’ve not seen this documented anywhere, though admittedly I’ve read little about the intimate details of 1.5 features. When using the new foreach syntax on a collection, you’ll run into trouble with code like this:
for(SomeObject o : someCollection) { if(!o.someCondition()) someCollection.remove(o); }
The above will throw a ConcurrentModificationException. I suppose that’s because the foreach is working with an Iterator behind the scenes, which of course isn’t happy that you tried to remove the object directly.
I don’t see this as being a bug, as that’s how iterators are supposed to behave - but it surely is annoying. It cost me about 30 minutes today trying to figure out how the hell I was concurrently modifying a list from a single thread. So whenever you need to conditionally remove an item from collection in a loop like that, use an iterator rather than the
foreach. And of course, use clear() to empty the whole thing at once after a loop.