Index: src/main/java/java/util/WeakHashMap.java =================================================================== --- src/main/java/java/util/WeakHashMap.java (revision 540993) +++ src/main/java/java/util/WeakHashMap.java (working copy) @@ -126,7 +126,7 @@ } public boolean hasNext() { - if (nextEntry != null) { + if (nextEntry != null && (nextKey != null || nextEntry.isNull)) { return true; } while (true) { Index: src/test/java/tests/api/java/util/WeakHashMapTest.java =================================================================== --- src/test/java/tests/api/java/util/WeakHashMapTest.java (revision 539409) +++ src/test/java/tests/api/java/util/WeakHashMapTest.java (working copy) @@ -297,6 +297,40 @@ .size()); } + /** + * Regression test for HARMONY-3883 + * @tests java.util.WeakHashMap#keySet() + */ + public void test_keySet_hasNext() { + WeakHashMap map = new WeakHashMap(); + ConstantHashClass cl = new ConstantHashClass(2); + map.put(new ConstantHashClass(1), null); + map.put(cl, null); + map.put(new ConstantHashClass(3), null); + Iterator iter = map.keySet().iterator(); + iter.next(); + iter.next(); + System.gc(); + assertFalse("Wrong hasNext() value", iter.hasNext()); + } + + static class ConstantHashClass { + private int id = 0; + + public ConstantHashClass(int id) { + this.id = id; + } + + public int hashCode() { + return 0; + } + + public String toString() { + return "ConstantHashClass[id=" + id + "]"; + } + } + + /** * @tests java.util.WeakHashMap#values() */