Remove all elements from a List in Java
This post will discuss how to remove all elements from a List in Java.
1. Using clear() method
The standard solution to remove all elements from a list is using the clear() method, which efficiently makes the list empty. Note that it only works on mutable lists, and throws UnsupportedOperationException for unmodifiable list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { List<String> collection = Stream.of("A", "B", "C", "D") .collect(Collectors.toList()); collection.clear(); System.out.println(collection); // [] } } |
The following is the source code of the clear() method for the ‘ArrayList’ implementation of the List interface. It sets the array buffer into which the elements of the List are stored to null. For tree-based List implementations, it simply sets the root to null.
|
1 2 3 4 5 6 |
public void clear() { modCount++; final Object[] es = elementData; for (int to = size, i = size = 0; i < to; i++) es[i] = null; } |
2. Using removeAll() method
An alternative idea is to call the removeAll() method, which removes all elements from a list that are present in the specified collection. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { List<String> collection = Stream.of("A", "B", "C", "D").collect(Collectors.toList()); collection.removeAll(collection); System.out.println(collection); // [] } } |
This approach is not recommended as it will be extremely slow for a large collection.
3. Using remove() method
Starting with Java 8, we can get a stream of elements in the list and call the remove() method for each element.
This is demonstrated below. Note that we have used a copy of the list to avoid java.util.ConcurrentModificationException, as Java doesn’t allow concurrent modification on the List instance while iterating over it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { List<String> collection = Stream.of("A", "B", "C", "D").collect(Collectors.toList()); List.copyOf(collection).stream().forEach(collection::remove); System.out.println(collection); // [] } } |
4. Using Iterator.remove() method
We can avoid creating a copy of the list by iterating over the list using a fail-safe iterator and calling the remove() method of the iterator. The iterator doesn’t throw ConcurrentModificationException when a thread modifies the list’s structure while another thread (or same thread) is iterating over it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { List<String> collection = Stream.of("A", "B", "C", "D").collect(Collectors.toList()); Iterator<String> iter = collection.listIterator(); while (iter.hasNext()) { iter.next(); iter.remove(); } System.out.println(collection); // [] } } |
That’s all about removing all elements from a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)