Index: lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java =================================================================== --- lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java (revision 1394156) +++ lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java (working copy) @@ -133,22 +133,22 @@ @Override public boolean hasNext() { - return nextIsSet ? true : setNext(); + return nextIsSet || setNext(); } @Override @SuppressWarnings("unchecked") public K next() { - if (nextIsSet || setNext()) { - try { - assert nextIsSet; - return (K) next; - } finally { - // release strong reference and invalidate current value: - nextIsSet = false; - next = null; - } + if (!hasNext()) { + throw new NoSuchElementException(); } - throw new NoSuchElementException(); + assert nextIsSet; + try { + return (K) next; + } finally { + // release strong reference and invalidate current value: + nextIsSet = false; + next = null; + } } @Override @@ -161,14 +161,15 @@ while (iterator.hasNext()) { next = iterator.next().get(); if (next == null) { - // already garbage collected! - continue; + // the key was already GCed, we can remove it from backing map: + iterator.remove(); + } else { + // unfold "null" special value: + if (next == NULL) { + next = null; + } + return nextIsSet = true; } - // unfold "null" special value - if (next == NULL) { - next = null; - } - return nextIsSet = true; } return false; }