If elements are added or removed from the underlying
container-managed collection used by an iterator other than by the java.util.Iterator.remove() method, the container should throw the
java.lang.IllegalStateException on the next operation on the iterator
Collection nySalesreps = nyOffice.getSalesreps();
Collection sfSalesreps = sfOffice.getSalesreps();
Iterator i = nySalesreps.iterator();
Salesrep salesrep;
// a wrong way to transfer the salesrep
while (i.hasNext()) {
salesrep = (Salesrep)i.next();
sfSalesreps.add(salesrep); // removes salesrep from nyOffice
}
// this is a correct and safe way to transfer the salesrep
while (i.hasNext()) {
salesrep = (Salesrep)i.next();
i.remove();
sfSalesreps.add(salesrep);
}