Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4416923

WeakHashMap.keySet ().toArray () can throw NoSuchElementException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • core-libs
    • None

      The implementation of AbstractCollection.toArray () is as follows:

      public Object[] toArray (Object a[]) {
          int size = size ();
          ...
          Iterator it = iterator ();
          for (int i=0; i < size; i++)
              a [i] = it.next ();
          ...
      }

      In case of WeakHashMap.keySet () (it is a collection), a situation when GC clears some weak keys after the size is obtained can occure (even if we synchronize on the WeakHashMap). It implies that java.util.NoSuchElementException is thrown, because the 'for' cycle expects 'size' elements in the iterator 'it'.

            mr Mark Reinhold
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: