Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is iterator.remove() is ROBUST in all cases in java?

Tags:

java

iteration

As in C++ iterator.remove() is not 100% safe or robust does java guarantees 100% robustness with iterator.remove()?

like image 660
Ashish Agarwal Avatar asked Feb 19 '26 09:02

Ashish Agarwal


1 Answers

Going off of matt's comment (who really deserves the credit)

http://java.sun.com/javase/6/docs/api/java/util/Iterator.html#remove%28%29

says:

void remove()

Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

So...... yes, there are conditions under which unexpected/unsafe behaviour can occur.

like image 122
Jonathan Fingland Avatar answered Feb 21 '26 22:02

Jonathan Fingland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!