What is the difference between ArrayList.clear() and ArrayList.removeAll()?
In Java, ArrayList.clear() and ArrayList.removeAll() are the two methods that are used to remove elements from an ArrayList, but they are used for different purposes. In this article will explain the difference between the two methods.
1. ArrayList.clear() Method
The clear() method of ArrayList in Java is used to remove all the elements from a list.
Syntax:
public void clear()
Parameters: The clear method does not need any parameters.
Return Type: It does not return any value as it removes all the elements in the list and makes it empty.
Example of ArrayList.clear() Method
This example shows the implementation of the ArrayList.clear() method.
import java.util.ArrayList;
// Driver Class
class GFG {
// Main Method
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// Adding Elements in the List
list.add("Geeks");
list.add("for");
list.add("geeks");
// Printing the List
System.out.println("The list initially: " + list);
// Clearing the List
list.clear();
// Empty List
System.out.println("The list after using clear() method: " +list);
}
}
Output
The list initially: [Geeks, for, geeks] The list after using clear() method: []
Explanation of the above Program:
In this example, we create a new ArrayList called "list" and add some elements to it using the add() method. Then, we print the elements of the ArrayList. Next, we call the clear() method to remove all elements from the ArrayList. Finally, we print the elements of the ArrayList again to demonstrate that it is now empty.
2. ArrayList.removeAll() method
This method is used to remove all the elements present in the collection from the specified list.
Syntax:
boolean removeAll(Collection c)
Parameters: This method has only argument, collection of which elements are to be removed from the given list.
Return Type: This method returns True if elements are removed and list changes.
Example of ArrayList.removeAll() Method
This example shows the implementation of the ArrayList.removeAll() method.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Arrays;
// Driver Class
class GFG{
// Main Method
public static void main(String[] args) {
ArrayList<String> result = new ArrayList<>();
// Adding Elements in ArrayList
result.add("1");
result.add("2");
result.add("3");
result.add("4");
// Creating Collection for Elements to be Removed
Collection<String> toRemove = Arrays.asList("1", "4");
// Removing Elements from the ArrayList
boolean output = result.removeAll(toRemove);
System.out.println("List after removeAll: " + result);
System.out.println("Is the elements are removed? " + output);
}
}
Output
List after removeAll: [2, 3] Is the elements are removed? true
Explanation of the above Program:
In this example, we create a new ArrayList called "result" add some elements to it using the add() method. Then we need to create a collection and provide a parameter into the collection which containing elements that are to be removed from the ArrayList. Next, we call the removeAll() method to remove elements from the ArrayList. Finally, we print the elements of the ArrayList again to demonstrate that the elements are removed. And You can print the boolean result also.
Difference between ArrayList.clear() and ArrayList.removeAll()
ArrayList.clear() | ArrayList.removeAll() | |
---|---|---|
Purpose | This method is used to remove all the elements. | This method is used to remove elements, which is present in arraylist and the collection both. |
Parameters | It does not takes any parameters. | It takes a collection as a parameter. |
Return Type | It does not return any value | It returns boolean (true if modified) value. |
Performance | Highly efficient | Depends on the size of the list and the collection |
Syntax | list.clear() | list.removeAll(collection) |
Time Complexity | O(n) | O(n * m) (n = size of list, m = size of collection) |
Space Complexity | O(1) | O(1) |