I believe this is the purpose behind the Iterator.remove() method, to be able to remove an element from the collection while iterating.
For example:
Iterator<String> iter = li.iterator();
while(iter.hasNext()){
if(iter.next().equalsIgnoreCase("str3"))
iter.remove();
}
More Related Contents:
- Why am I not getting a java.util.ConcurrentModificationException in this example?
- java how to convert string to String[] and put in Specific string variabel
- Iterate through a possibly infinite amount of nested ArrayLists
- How to create a Java ArrayList in C
- Java: convert List to a join()d String
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- Convert list to array in Java [duplicate]
- How do I join two lists in Java?
- Java Compare Two Lists
- Converting array to list in Java
- Concurrent Modification exception [duplicate]
- Java List.add() UnsupportedOperationException
- Java List.contains(Object with field value equal to x)
- java: (String[])List.toArray() gives ClassCastException
- How to randomize two ArrayLists in the same fashion?
- Remove duplicates from a list of objects based on property in Java 8 [duplicate]
- Java 8 stream reverse order
- How can I return the difference between two lists?
- Cannot instantiate the type List [duplicate]
- Android: How to disable list items on list creation
- Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?
- How to modify a Collection while iterating using for-each loop without ConcurrentModificationException? [duplicate]
- Simple way to compare 2 ArrayLists
- Create a List of primitive int?
- How to convert a List into a comma separated string without iterating List explicitly [duplicate]
- ArrayList vs LinkedList from memory allocation perspective
- Finding out if a list of Objects contains something with a specified field value?
- new ArrayList() failing in Java
- Is LinkedList thread-safe when I’m accessing it with offer and poll exclusively?
- Get last element of Stream/List in a one-liner