Collection clear() method in Java with Examples
Last Updated :
16 Feb, 2024
The collection clear() method of Java Collection Interface clears the Collection upon which it is called. After this method is called, the collection will be empty as it removes all the elements from the collection.
This method does not take any parameter and does not return any value.
Example:
Java
// Java program to demonstrate the
// usage of clear() method in ArrayList
import java.util.ArrayList;
import java.util.Arrays;
public class ClearMethodExample {
public static void main(String[] args) {
// Create an ArrayList and initialize it with some elements
ArrayList<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));
// Display the list before clearing
System.out.println("List before clearing: " + list);
// Clear the list
list.clear();
// Display the list after clearing
System.out.println("List after clearing: " + list);
}
}
Output:
List before clearing: [1, 2, 3, 4]
List after clearing: []
Syntax
Collection.clear()
Parameters:
- This method does not accept any parameter
Returns:
- This method does not return any value.
Exceptions:
- UnsupportedOperationException: if the add operation is not supported by this collection
Collection clear() Method Examples
Let's see some examples of how to use the collection clear() method in Java.
Example 1:
How to remove all elements from a collection using collection clear() Method
Java
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String args[]) {
// creating an empty LinkedList
Collection<String> list = new LinkedList<>();
// use add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
// Output the present list
System.out.println("The list is: " + list);
// Clearing the LinkedList
list.clear();
// printing the new list
System.out.println("The new List is: " + list);
}
}
OutputThe list is: [Geeks, for, Geeks]
The new List is: []
Example 2:
Using the clear() method on ArrayDeque to remove its elements.
Java
// Java code to illustrate clear() method
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Collection<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Clearing the ArrayDeque
de_que.clear();
// printing the new ArrayDeque
System.out.println("The new ArrayDeque is: "
+ de_que);
}
}
OutputArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The new ArrayDeque is: []
Example 3:
Using the clear() method on ArrayList
Java
// Java code to illustrate clear() method
import java.io.*;
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args)
{
// create an empty array list with an initial capacity
Collection<Integer> arrlist = new ArrayList<Integer>(5);
// use add() method to add elements in the list
arrlist.add(15);
arrlist.add(20);
arrlist.add(25);
// prints all the elements available in list
System.out.println("ArrayList: " + arrlist);
// Clearing the ArrayList
arrlist.clear();
// printing the new ArrayList
System.out.println("The new ArrayList is: "
+ arrlist);
}
}
OutputArrayList: [15, 20, 25]
The new ArrayList is: []
Reference: Official Oracle Documents
Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of the collection clear function and its uses in Java.
The clear method in Java is a fundamental function for collection manipulation. With this guide, you can easily remove all elements of a collection using the clear function.
Read More Collection Methods
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read