This example shows how to clear LinkedList in Java (empty or remove all elements). This example also shows different ways to remove all elements from the Java LinkedList.
How to clear LinkedList in Java?
There are several ways using which we can empty the LinkedList in Java.
1. Using the clear method
The clear
method of the LinkedList class removes all elements from the LinkedList.
1 |
public void clear() |
The LinkedList becomes empty after this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.LinkedList; public class LinkedListClearExample { public static void main(String[] args) { //create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); System.out.println("Is linked list empty? " + linkedListColors.isEmpty()); /* * To clear the LinkedList, use the * clear method */ linkedListColors.clear(); System.out.println("Is linked list empty? " + linkedListColors.isEmpty()); } } |
Output
1 2 |
Is linked list empty? false Is linked list empty? true |
2. Using the removeAll method
You can also use the removeAll
method of the LinkedList class to remove all elements from the LinkedList.
1 |
public boolean removeAll(Collection<?> c) |
The removeAll
method is inherited from the AbstractCollection class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); System.out.println("Is linked list empty? " + linkedListColors.isEmpty()); /* * The removeAll method removes all the elements * of this list which are also present in another * list. * * Here we will pass the same linked list * object to remove all the elements. */ linkedListColors.removeAll(linkedListColors); System.out.println("Is linked list empty? " + linkedListColors.isEmpty()); |
Output
1 2 |
Is linked list empty? false Is linked list empty? true |
3. Using the remove method of the Iterator
We can remove all the elements of the list while iterating it using the remove
method of the Iterator as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); System.out.println("LinkedList contains: " + linkedListColors); //get an iterator Iterator<String> iterator = linkedListColors.iterator(); while(iterator.hasNext()){ iterator.next(); //remove current element using the remove method iterator.remove(); } System.out.println("LinkedList contains: " + linkedListColors); |
Output
1 2 |
LinkedList contains: [Red, Green, Blue] LinkedList contains: [] |
4. By assigning a new object to the reference
You can simply reassign a new empty linked list object to the existing reference as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); System.out.println("LinkedList contains: " + linkedListColors); //assign a new empty linked list object linkedListColors = new LinkedList<String>(); System.out.println("LinkedList contains: " + linkedListColors); |
Output
1 2 |
LinkedList contains: [Red, Green, Blue] LinkedList contains: [] |
Which method should I use to clear the LinkedList?
The first three approaches iterate the linked list in order to remove all the elements from it. While this is ok for the list having a small number of elements, but it can affect the performance if the number of elements contained in the linked list is very large.
The fourth approach reassigns the existing reference to a new empty linked list object. It involves a costly object creation operation and leaves the other work to be done by the garbage collection thread instead of the main thread.
This example is a part of the Java LinkedList tutorial.
Please let me know your views in the comments section below.