I'm just curious how several iterators on the same object will behave and enter the following code.
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
class Test{
public static void main(String[] a) {
Map<Integer,String> map = new HashMap<>();
map.put(1,"One");
Iterator<Integer> it1 = map.keySet().iterator();
Iterator<Integer> it2 = map.keySet().iterator();
it1.next();
it1.remove();
System.out.println(it2.hasNext());
System.out.println(it2.next());
System.out.println(map.get(1));
}
}
The card is empty as expected. But I thought I would Iterator#hasNextreturn false, but instead I returned trueand Iterator#nextthrew a ConcurrentModificationException.
when a value is removed from Collectionusing the method Iterator#remove, should the other iterator method not hasNextreturn false since there would be no value to be returned?
source
share