Remove elements from a List
Java List provides several methods for removing elements: remove(index), remove(Object), removeAll(), and the Java 8 removeIf() with a predicate. This article covers each approach with examples, including safe removal during iteration using Iterator.remove() to avoid ConcurrentModificationException.
ListArrayListLinkedList
Remove list elements using element's index
To remove an element from List using it's index, we can use the List.remove(int i)
public void deleteUsingIndexRemoveMethod() {
List<Integer> arrayList = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
arrayList.remove(0);
System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
}
In the above example, we are removing element at index 0
AFTER - arrayList size : 9 , elements : [2, 3, 4, 5, 6, 7, 8, 9, 10]
IndexOutOfBoundsExceptionRemove list elements using element's value
To delete an element from List using it's value, you can use List.remove(Object element)
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);
}
In the above example, we are removing element with value as "a" from the list. Note that this method only removes the first occurrence of the element. When you run the program, you will see below output.
AFTER - arrayList size : 3 , elements : [b, c, a]
To find the element to remove from list, it checks for element from list that is equal to the element
that you are passing as parameter, so it's important that Object you are using have equals(Object other)hashCode()Object
See this article for implementing equals and hashCode
Remove list elements using removeIf method and Lambda function
The removeIf
One major difference between previous remove(Object element)
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".
AFTER - arrayList size : 2 , elements : [b, c]
Remove list elements using Iterator
You can obtain Iteratoriterator()listIterator()remove()
public void deleteUsingIteratorMethod() {
List<String> arrayList = new ArrayList<>(List.of("a", "b", "c", "a"));
System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
Iterator<String> ite = arrayList.iterator();
while(ite.hasNext()){
String element = ite.next();
if("a".equals(element)) {
ite.remove();
}
}
System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList);
}
When you run above program, it will produce following output.
AFTER - arrayList size : 2 , elements : [b, c]