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();
}
}