Hi yes it will give an exception as
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at coffee.CoffeeApp.main(CoffeeApp.java:20)
Java Result: 1
because
fail-fast behavior is implemented by keeping a modification count and if iteration thread realizes the change in modification count it throws ConcurrentModificationException.
so do something like this and iterate over list with the help of Iterator
List<String> list = new ArrayList<String>();
list.add("Something");//add only 1 object
Iterator<String> it = list.iterator();
while(it.hasNext()) {
it.next();
it.remove();
//Do something with obj
}
what we can say is the fail-fast behavior of an iterator cannot be guaranteed as it is, here an unsynchronized concurrent modification is there. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, fail-fast Iterators fail as soon as they realized that structure of Collection has been modified since iteration has begun and if the iterator thread realizes the modification ,then it throws ConcurrentModificationException. Changes can be additions, updation or deletion.
In android may be it is under different implementation so not having a problem.
for a comparison study please visit however i did not had a great R&D here
http://www.docjar.com/html/api/java/util/ArrayList.java.html
v/s
https://android.googlesource.com/platform/libcore/+/f33eae7e84eb6d3b0f4e86b59605bb3de73009f3/luni/src/main/java/java/util/ArrayList.java