In this article, we are going to look at different ways to remove elements from List while iterating.
If you are using a list which is not thread safe (for example ArrayList, LinkedList) and try to
remove an element while iterating then it will throw a ConcurrentModificationException.
Below solutions will address that.
- Remove list elements using Iterator while iterating through List
- Remove list elements using removeIf method
You can obtain Iterator using iterator() method or listIterator() from List, then call remove() on Iterator.
Example 1 : Remove element if the value is "c" from List.
public void deleteWhileIteratingListUsingIterator() {
List<String> arrayList = new ArrayList<>(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"));
System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
Iterator<String> ite = arrayList.iterator();
while(ite.hasNext()) {
String element = ite.next();
if("c".equals(element)) {
ite.remove(); // removes current element from iterator position
}
}
System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
}
When you run above program, it will produce following output.
BEFORE - arrayList size : 11 , elements : [a, b, c, d, e, f, g, h, i, j, k]
AFTER - arrayList size : 10 , elements : [a, b, d, e, f, g, h, i, j, k]
Example 2 : Remove elements from List which are at even position.
public void deleteElementsAtEvenPositionUsingIterator() {
List<Integer> arrayList = new ArrayList<>(List.of(0,1,2,3,4,5,6,7,8,9));
System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
Iterator<Integer> ite = arrayList.iterator();
int index =0;
while(ite.hasNext()) {
ite.next();
if( (index++) % 2 == 0) {
ite.remove(); // removes current element from iterator position
}
}
System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
}
When you run above program, it will produce following output.
BEFORE - arrayList size : 10 , elements : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
AFTER - arrayList size : 5 , elements : [1, 3, 5, 7, 9]
Note that only when you call Iterator.next() method, it moves the cursor to current position.
So, before removing the element, it is necessary to make a call to this method.
The removeIf method also uses Iterator internally, and you can pass a Lambda function as Predicate,
and using this method reduces number of lines of code you need.
This method is handy when you want to remove elements based on List's elements and not based on their index.
public void deleteUsingRemoveIfMethod() {
List<String> arrayList = new ArrayList<>(List.of("a", "b", "c", "a"));
System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
arrayList.removeIf(s -> "a".equals(s));
System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
}
When you run the above program, it will produce the below output, as you notice it removed all occurrences of
element "a".
BEFORE - arrayList size : 4 , elements : [a, b, c, a]
AFTER - arrayList size : 2 , elements : [b, c]
Similar Articles
-
How to loop through a List elements in different ways
-
How to remove elements from a List