Iterator<Entry<V>> () { final Iterator<Entry<IntReference<V>>> entryIterator = ((Iterable<Entry<IntReference<V>>>)myMap.entrySet()).iterator(); return new Iterator<>() { private Entry<V> nextVEntry; private Entry<IntReference<V>> nextReferenceEntry; private Entry<IntReference<V>> lastReturned; { nextAliveEntry(); } @Override public boolean hasNext() { return nextVEntry != null; } @Override public Entry<V> next() { if (!hasNext()) throw new NoSuchElementException(); Entry<V> result = nextVEntry; lastReturned = nextReferenceEntry; nextAliveEntry(); return result; } private void nextAliveEntry() { while (entryIterator.hasNext()) { Entry<IntReference<V>> entry = entryIterator.next(); final V v = entry.getValue().get(); if (v == null) { continue; } final int key = entry.getKey(); nextVEntry = new SimpleEntry<>(key, v); nextReferenceEntry = entry; return; } nextVEntry = null; } @Override public void remove() { Entry<IntReference<V>> last = lastReturned; if (last == null) throw new NoSuchElementException(); myMap.replaceNode(last.getKey(), null, last.getValue()); } }; }