// iterate safely (& quickly) in the face of concurrent modifications static IterableIterator concurrentlyIterateSet(final NavigableSet set) { fO mutex = collectionMutex(set); class ConcurrentlyIterateSet extends F0 { Iterator it = set.iterator(); A key; A get() { synchronized(mutex) { try { if (!it.hasNext()) null; ret key = it.next(); } catch (ConcurrentModificationException e) { print("Re-iterating"); it = set.tailSet(key, false).iterator(); if (!it.hasNext()) null; ret key = it.next(); // Can't throw another exception } } } } ret iteratorFromFunction(new ConcurrentlyIterateSet); }