Examples below show how to remove element from a ArrayList
There are various ways of removing an item from a ArrayList
type in Java as explained below.
1) Remove element from ArrayList using Java 8
Java 8 provides removeIf(Predicate)
method in Collection
interface that takes Predicate
as an argument and it removes all the entries in the ArrayList
which match the given Predicate
. In below example users.removeIf(u -> u.equals("Harry"));
removes the String
"Harry" from the ArrayList
.
import java.util.ArrayList; import java.util.Iterator; import java.util.List; // Removes element from a ArrayList public class MyClass { public static void main(String[] args) { List<String> users = new ArrayList<String>(); users.add("Harry"); users.add("IronMan"); users.add("Batman"); users.removeIf(u -> u.equals("Harry")); System.out.println("users " + users); } }
Output :
users [IronMan, Batman]
2) Remove element from ArrayList while iterating using Iterator remove()
While looping over an ArrayList
, it is recommended to use Iterator remove()
for removing an element from ArrayList
as you will not get ConcurrentModificationException
. If the underlying ArrayList
is modified while removing the element, then the results are not predictable.
import java.util.ArrayList; import java.util.Iterator; import java.util.List; // Removes element from a List public class MyClass { public static void main(String[] args) { List<String> users = new ArrayList<String>(); users.add("Harry"); users.add("IronMan"); users.add("Batman"); Iterator<String> itr = users.iterator(); while (itr.hasNext()) { String user = itr.next(); if (user.equalsIgnoreCase("Harry")) { itr.remove(); // Remove entry from ArrayList } } System.out.println("users " + users); } }
Output :
users [IronMan, Batman]
Code below throws ConcurrentModificationException
import java.util.ArrayList; import java.util.Iterator; import java.util.List; // Removes element from a List public class MyClass { public static void main(String[] args) { List<String> users = new ArrayList<String>(); users.add("Harry"); users.add("IronMan"); users.add("Batman"); for (String user : users) { if (user.equalsIgnoreCase("Harry")) { users.remove(user); } } System.out.println("users " + users); } }
Output :
Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at MyClass.main(MyClass.java:12)
3) Using List remove(int index)
You can remove element from ArrayList
by using List remove(int index)
method. It will use the index at which the element is stored in the ArrayList
to remove the element. Below example shows how to remove entry from List using List remove(int index)
method.
import java.util.ArrayList; import java.util.List; // Removes entry from ArrayList using remove() public class MyClass { public static void main(String[] args) { List<String> celebrities = new ArrayList<String>(); celebrities.add("Harry"); celebrities.add("IronMan"); celebrities.add("Batman"); celebrities.remove(1); // Remove entry from ArrayList System.out.println("celebrities " + celebrities); } }
Output :
celebrities [Harry, Batman]
4) Using List remove(Object object)
You can remove element from ArrayList
by using List remove(Object object)
method. It checks the existence of element by using: Object equals()
and removes the first element that matches the element in ArrayList
.
Below example shows how to remove entry from List using List remove(Object object)
method.
import java.util.ArrayList; import java.util.List; // Removes element from ArrayList using List remove(Object object) public class MyClass { public static void main(String[] args) { List<String> celebrities = new ArrayList<String>(); celebrities.add("Harry"); celebrities.add("IronMan"); celebrities.add("Batman"); celebrities.remove("Harry"); // Remove entry from ArrayList celebrities.forEach(System.out::println); } }
Output :
IronMan Batman
5) Remove all elements from ArrayList using List removeAll()
You can remove all elements from ArrayList
by using List removeAll()
method. It removes all of the elements from the ArrayList
. List removeAll()
has time complexity of O(N^2)Below example shows how to remove all the elements from a List using List removeAll()
method.
import java.util.ArrayList; import java.util.List; // Remove all elements from ArrayList public class MyClass { public static void main(String[] args) { List<String> users = new ArrayList<String>(); users.add("Harry"); users.add("IronMan"); users.add("Batman"); users.removeAll(users); System.out.println("users " + users); } }
Output :
users []
6) Remove all elements from ArrayList using List clear()
You can remove element from ArrayList
by using List clear()
method. It removes all of the elements from the list . The list will be empty after this call returns. List clear() has time complexity of O(N). Below example shows how to remove all the elements from a ArrayList
using List clear()
method.
import java.util.ArrayList; import java.util.List; // Remove all elements from ArrayList public class MyClass { public static void main(String[] args) { List<String> users = new ArrayList<String>(); users.add("Harry"); users.add("IronMan"); users.add("Batman"); users.clear(); System.out.println("users " + users); } }
Output :
users []
References :
Oracle Docs List remove()
Oracle Docs Iterator remove()
Oracle Docs List remove(Object object)
Oracle Docs List removeAll()
Oracle Docs List clear()