Remove all elements from the ArrayList in Java Last Updated : 11 Dec, 2018 Comments Improve Suggest changes Like Article Like Report Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove all elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 3, 4] Output: ArrayList = [] Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: ArrayList = [] Using clear() method: Syntax: collection_name.clear(); Code of clear() method: public void clear() { for (int i = 0; i < size; i++) list[i] = null; size = 0; } Below is the implementation of the above approach: Java // Java Program for remove all elements ArrayList // Using clear() method // import ArrayList package import java.util.ArrayList; public class GFG { // main method public static void main(String[] args) { // create empty ArrayList ArrayList<String> list = new ArrayList<>(); // Adding elements of list list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("Gaurav"); // printing initial value ArrayList System.out.println("ArrayList: " + list); // print size of ArrayList System.out.println("Size of ArrayList = " + list.size()); // remove all elements using clear() method list.clear(); // printing ArrayList System.out.println("\nAfter clear\n\n" + "ArrayList: " + list); // print size of ArrayList after clear list System.out.println("Size of ArrayList = " + list.size()); } } Output: ArrayList: [Geeks, for, Geeks, Gaurav] Size of ArrayList = 4 After clear ArrayList: [] Size of ArrayList = 0 Time Complexity: O(N) Using removeAll() method Syntax: collection_name.removeAll(collection_name); Code of removeAll() method: public boolean removeAll(Collection list) { boolean isModi = false; Iterator ite= iterator(); while (ite.hasNext()) { if (list.contains(ite.next())) { ite.remove(); isModi = true; } } return isModi; } Below is the implementation of the above approach: Java // Java Program for remove all elements ArrayList // Using removeAll() method // import ArrayList package import java.util.ArrayList; public class GFG { // main method public static void main(String[] args) { // create empty ArrayList ArrayList<String> list = new ArrayList<>(); // Adding elements of list list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("Gaurav"); // printing initial value ArrayList System.out.println("ArrayList: " + list); // print size of ArrayList System.out.println("Size of ArrayList = " + list.size()); // remove all elements using clear() method list.removeAll(list); // printing ArrayList System.out.println("\nAfter clear\n\n" + "ArrayList: " + list); // print size of ArrayList after clear list System.out.println("Size of ArrayList = " + list.size()); } } Output: ArrayList: [Geeks, for, Geeks, Gaurav] Size of ArrayList = 4 After clear ArrayList: [] Size of ArrayList = 0 Time Complexity: O(N^2) Comment More infoAdvertise with us Next Article Remove all elements from the ArrayList in Java R rajput-ji Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2018 Java-Collections Java-ArrayList Java-List-Programs +2 More Practice Tags : JavaJava-Collections Similar Reads How to remove an element from ArrayList in Java? ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. With 4 min read Remove repeated elements from ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove repeated elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 2, 3, 4, 4, 4] Output: [1, 2, 3, 4] Input: ArrayList = [12, 23, 23, 34, 45, 45, 45, 45, 57, 67, 89] Output: [12, 23, 34, 45, 57, 67, 89] Below are 3 min read How to Remove Duplicates from ArrayList in Java Given an ArrayList with duplicate values, the task is to remove the duplicate values from this ArrayList in Java. Examples: Input: List = [1, 10, 2, 2, 10, 3, 3, 3, 4, 5, 5] Output: List = [1, 10, 2, 3, 4, 5] Input: List = ["G", "e", "e", "k", "s"] Output: List = ["G", "e", "k", "s"] Using Iterator 4 min read ArrayList removeIf() Method in Java with Examples The Java ArrayList removeIf() method is used to remove all elements from the ArrayList that satisfy a given predicate filter. The predicate is passed as a parameter to the method, and any runtime exceptions thrown during iteration or by the predicate are passed to the caller.The removeIf() method us 2 min read ArrayList removeAll() Method in Java with Examples The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList that are specified in another collection or within the same list itself.Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argument 3 min read Like