Say you want to filter elements out of a List. Running this code triggers something like a concurrency exception as the list is being modified during the loop:
List<String> aList = new ArrayList<String>();
// Add instances to list here ...
for( String s : aList ){
if( "apple".equals( s ) ){ // Criteria
aList.remove(s);
}
}
What is the conventional way of doing this and what other approaches do you know of?
best way is to use iterator.remove()