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

HashMap.entrySet().iterator() does not raise specified exception

XMLWordPrintable

    • beta2
    • sparc
    • solaris_2.6
    • Verified



      Name: auR10023 Date: 06/22/2001



      HashMap.entrySet.iterator() does not raise ConcurrentModificationException
      if content of the entry set changes after iterator has been created.

      The javadoc for HashMap says:

      ....
      The iterators returned by all of this class's "collection view methods" are
      fail-fast: if the map is structurally modified at any time after the iterator is
      created, in any way except through the iterator's own remove or add methods, the
      iterator will throw a ConcurrentModificationException. Thus, in the face of
      concurrent modification, the iterator fails quickly and cleanly, rather than
      risking arbitrary, non-deterministic behavior at an undetermined time in the
      future.
      ...
              
      Here is the example:
              
      class t {
          public static void main(String args[]) {
              HashMap map = new HashMap();
              Iterator iter = null;
              HashMap map1 = new HashMap();
              
              map.put("key", "value");
              iter = map1.entrySet().iterator();
              map1.putAll(map);
                  
              try {
                  iter.next();
                  System.out.println("ConcurrentModificationException was not " +
                                     "thrown after changing of " +
                                     "the HashMap by putAll() method");
                  return;
              } catch (ConcurrentModificationException e) {
              } catch (NoSuchElementException e) {
                  System.out.println("NoSuchElementException was thrown instead of " +
                                     "ConcurrentModificationException ");
                  return;
              }
          }
      }

      #java -version

      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

      #java t

      NoSuchElementException was thrown instead of ConcurrentModificationException

      Following methods have the same troubles:

      HashMap.keySet().iterator()
      HashMap.values().iterator()

      ======================================================================

            jjb Josh Bloch
            avusunw Avu Avu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: