Index: src/main/java/java/util/WeakHashMap.java =================================================================== --- src/main/java/java/util/WeakHashMap.java (revision 539313) +++ src/main/java/java/util/WeakHashMap.java (working copy) @@ -178,6 +178,29 @@ throw new ConcurrentModificationException(); } } + + /** + * Adds next element to the given collection. + * + * @param coll Collection to add next element to + * + * @return true if next element exists and was added, false otherwise + */ + private boolean addNext(Collection coll) { + if (expectedModCount == modCount) { + if (hasNext()) { + currentEntry = nextEntry; + nextEntry = currentEntry.next; + R result = type.get(currentEntry); + // free the key + nextKey = null; + coll.add(result); + return true; + } + return false; + } + throw new ConcurrentModificationException(); + } } /** @@ -387,20 +410,18 @@ @Override public Object[] toArray() { Collection coll = new ArrayList(size()); + HashIterator iter = (HashIterator) iterator(); - for (Iterator iter = iterator(); iter.hasNext();) { - coll.add(iter.next()); - } + while(iter.addNext(coll)); return coll.toArray(); } @Override public T[] toArray(T[] contents) { Collection coll = new ArrayList(size()); + HashIterator iter = (HashIterator) iterator(); - for (Iterator iter = iterator(); iter.hasNext();) { - coll.add(iter.next()); - } + while(iter.addNext(coll)); return coll.toArray(contents); } };