// iterate safely (& quickly) in the face of concurrent modifications static IterableIterator concurrentlyIterateKeys(final NavigableMap map) { ret iteratorFromFunction(new F0() { Iterator it = keys(map).iterator(); A key; A get() { synchronized(map) { try { if (!it.hasNext()) null; ret key = it.next(); } catch (ConcurrentModificationException e) { print("Re-iterating"); it = map.tailMap(key, false).keySet().iterator(); if (!it.hasNext()) null; ret key = it.next(); // Can't throw another exception } } } }); }