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

Iterator() created before ConcurrentHashMap remove() still showing the removed first entry

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      JDK SE 1.8

      A DESCRIPTION OF THE PROBLEM :
      I created an iterator() and then removed the 1st entry form the map before iterating it, I always get 1st item present in the iterator, But when I remove 2nd or proceeding entries, the current iterator removes that entry.

      Example of removing 1st entry form map:

          Map<Integer,Integer> m1 = new ConcurrentHashMap();
          m1.put(4, 1);
          m1.put(5, 2);
          m1.put(6, 3);
          Iterator i1 = m1.entrySet().iterator();
          m1.remove(4); // remove entry from map
          while (i1.hasNext())
              System.out.println("value :: "+i1.next()); //still shows entry 4=1
      and the output is:

      value :: 4=1
       value :: 5=2
       value :: 6=3

      Example of removing 3rd entry form map:

          Map<Integer,Integer> m1 = new ConcurrentHashMap();
          m1.put(4, 1);
          m1.put(5, 2);
          m1.put(6, 3);
          Iterator i1 = m1.entrySet().iterator();
          m1.remove(6); // remove entry from map
          while (i1.hasNext())
              System.out.println("value :: "+i1.next()); //does not shows entry 6=3
      and the output is:

      value :: 4=1
       value :: 5=2

      Why removing 1st entry from map does not reflect but removing 2nd or proceeding entries reflect in iterator()?

      While https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html says:

      Iterators, Spliterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException.

      That means, its iterators reflect the state of the hash table at point of the creation of the iterator. And when we remove/add an entry from the Map, the Iterator will show the originally obtained entries from map?


      FREQUENCY : always


            martin Martin Buchholz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: