1 Reply Latest reply: Nov 10, 2007 8:24 AM by Greybird-Oracle RSS

    Bug (?) report: using remove on entrySet iterator

    593557
      I'm using BDB JE 3.2.44 (most recent stable release)

      When I have the following piece of code using the Collections library:
           public void dumpAndDestroy() {
                System.out.println("Dumping word follow database ...");
                Iterator<Entry<String, String>> entries = wordMap.entrySet().iterator();
                while (entries.hasNext()) {
                     Entry<String, String> e = entries.next();
                     System.out.println(e.getKey() + " " + e.getValue());
                     entries.remove();
                }
           }
      I get an Array index out of bounds exception after the last element in the collection has been deleted.
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
           at com.sleepycat.collections.BlockIterator.hasNext(BlockIterator.java:303)
           at WordFollows.dumpAndDestroy(WordFollows.java:82)
           at WordFollows.main(WordFollows.java:104)
      What I would rather expect would be that the "hasNext()" method would return false if there are no more elements. So I gather this is a bug... right?

      I can work around it by checking whether I'm at the end before deleting the element, but it seems a bit contrived that I should have to do that.

      Workaround:
           public void dumpAndDestroy() {
                System.out.println("Dumping word follow database ...");
                Iterator<Entry<String, String>> entries = wordMap.entrySet().iterator();
                boolean cont = entries.hasNext();
                while (cont) {
                     Entry<String, String> e = entries.next();
                     System.out.println(e.getKey() + " " + e.getValue());
                     cont = entries.hasNext();
                     entries.remove();
                }
           }
        • 1. Re: Bug (?) report: using remove on entrySet iterator
          Greybird-Oracle
          Hi Kris,
          What I would rather expect would be that the
          "hasNext()" method would return false if there are no
          more elements. So I gather this is a bug... right?
          Yes, hasNext() should absolutely work (return false and not throw an exception) after calling remove(). So yes, this appears to be a bug. We will make sure this is fixed in a future release.

          Thank you for reporting this!

          --mark