Java LinkedList clear()
method
The clear()
method in Java's LinkedList
class is a handy tool for quickly removing all elements from the list. It essentially empties the linked list, making it as if it were newly created.
Syntax
public void clear()
Parameters
Parameter | Description |
---|---|
None | This method doesn't accept any parameters. |
Return Value
The clear()
method returns void
, meaning it doesn’t explicitly return a value. Instead, it modifies the list in place.
Examples
Example 1: Clearing an Empty List
Let's start with a simple case – clearing a LinkedList that is already empty. This demonstrates that calling clear()
on an empty list has no effect.
LinkedList<String> myList = new LinkedList<>();
System.out.println("Size before clear: " + myList.size()); // Output will be 0
myList.clear();
System.out.println("Size after clear: " + myList.size()); // Output will still be 0
Size before clear: 0
Size after clear: 0
As you can see, the size remains zero because the list was already empty.
Example 2: Clearing a List with Elements
Now let's look at what happens when we clear a LinkedList that actually contains elements. The method removes all elements, resulting in an empty list.
LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
System.out.println("Size before clear: " + numbers.size()); // Output will be 3
numbers.clear();
System.out.println("Size after clear: " + numbers.size()); // Output will be 0
Size before clear: 3
Size after clear: 0
The list initially held three elements, but after calling clear()
, the size is reduced to zero. The elements are removed from memory.
Example 3: Clearing a List and Iterating
This example demonstrates how clearing a LinkedList affects any iterators that might be associated with it. It's crucial to understand this because using an iterator after the list has been cleared will result in a ConcurrentModificationException
.
LinkedList<String> names = new LinkedList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
System.out.println(name);
}
names.clear(); // Clear the list
// The following line will throw a ConcurrentModificationException:
// iterator.next();
The commented-out iterator.next()
call would cause a ConcurrentModificationException
because the linked list has been modified while the iterator was in use. If you need to process elements after clearing, create a new iterator.
Example 4: Clearing and Adding New Elements
This example shows that after calling clear()
, you can add new elements back into the LinkedList as if it were newly created.
LinkedList<Double> values = new LinkedList<>();
values.add(10.5);
values.add(20.7);
values.clear();
System.out.println("Size after clear: " + values.size()); // Output will be 0
values.add(30.9);
values.add(40.2);
System.out.println("Size after adding new elements: " + values.size()); // Output will be 2
Size after clear: 0
Size after adding new elements: 2
The list is first cleared, then two new values are added back in.