The clear() method in Java is used to remove all elements from a List. After calling this method, the list becomes empty, but the list object itself still exists and can be reused.
- It removes all elements from the list.
- After execution, the size of the list becomes 0.
import java.util.ArrayList;
import java.util.List;
public class Geeks{
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Aman");
names.add("Riya");
names.add("Karan");
System.out.println("Before clear(): " + names);
names.clear();
System.out.println("After clear(): " + names);
}
}
Output
Before clear(): [Aman, Riya, Karan] After clear(): []
Explanation: The clear() method removes all elements from the names list. After calling it, the list becomes empty, but the same list object can still be used to add new elements.
Syntax
public void clear()
- Parameter: This method accepts does not accepts any parameter.
- Return Value: The return type of the function is void and it does not returns anything.
- Exceptions: This method throws an UnsupportedOperationException if the clear() operation is not supported by this list.
How clear() Works in List
The clear() method is declared in the Collection interface and is available to List implementations through inheritance.
When clear() is called:
- All elements are removed from the list.
- The size of the list becomes 0.
- The list object remains available for reuse.
clear() Method with Different List Implementations
The clear() method can be used with different classes that implement the List interface.
Common implementations include:
1. clear() with ArrayList
ArrayList stores elements in a dynamic array. Calling clear() removes all stored elements from the list.
import java.util.ArrayList;
public class Geeks{
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println("Before clear(): " + list);
list.clear();
System.out.println("After clear(): " + list);
System.out.println("Size: " + list.size());
}
}
Output
Before clear(): [10, 20, 30] After clear(): [] Size: 0
Explanation: All elements are removed from the ArrayList, and its size becomes 0.
2. clear() with LinkedList
LinkedList stores elements as linked nodes. The clear() method removes all nodes from the list.
import java.util.LinkedList;
public class Geeks{
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println("Before clear(): " + list);
list.clear();
System.out.println("After clear(): " + list);
}
}
Output
Before clear(): [Java, Python, C++] After clear(): []
Explanation: The clear() method removes all elements from the LinkedList, leaving it empty.
3. clear() with Vector
Vector is a synchronized implementation of the List interface. It also supports the clear() method.
import java.util.Vector;
public class Geeks{
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("A");
vector.add("B");
vector.add("C");
System.out.println("Before clear(): " + vector);
vector.clear();
System.out.println("After clear(): " + vector);
}
}
Output
Before clear(): [A, B, C] After clear(): []
Explanation: All elements are removed from the Vector, but the Vector object remains available for reuse.
4. clear() with Stack
Stack extends Vector, so it also inherits the clear() method.
import java.util.Stack;
public class Geeks{
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Before clear(): " + stack);
stack.clear();
System.out.println("After clear(): " + stack);
}
}
Output
Before clear(): [10, 20, 30] After clear(): []
Explanation: The method removes every element from the stack and makes it empty.
Reusing a List After clear()
Calling clear() does not delete the list object. New elements can be added again.
import java.util.ArrayList;
public class Geeks{
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.clear();
list.add("C++");
list.add("JavaScript");
System.out.println(list);
}
}
Output
[C++, JavaScript]
Explanation: The original elements are removed, and the same list is reused to store new elements.
clear() vs removeAll()
Both methods can remove elements, but they are used differently.
| clear() | removeAll() |
|---|---|
| Removes all elements from a collection | Removes only elements present in another collection |
| Does not take arguments | Takes another collection as an argument |
| Makes the list completely empty | May leave some elements in the list |