Thursday, July 4, 2024
HomeLanguagesJavaCollection clear() method in Java with Examples

Collection clear() method in Java with Examples

The clear() of java.util.Collection interface is used to clear the Collection upon which it is called. This method does not take any parameter and does not returns any value.

Syntax:

Collection.clear()

Parameters: This method do not accept any parameter

Return Value: This method does not return any value.

Exceptions: This method throws following exceptions:

  • UnsupportedOperationException: if the add operation is not supported by this collection

Below examples illustrate the Collection clear() method:

Example 1: Using LinkedList Class




// Java code to illustrate boolean clear() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
  
        // creating an empty LinkedList
        Collection<String> list = new LinkedList<String>();
  
        // 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);
    }
}


Output:

The list is: [Geeks, for, Geeks]
The new List is: []

Example 2: Using ArrayDeque Class




// 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);
    }
}


Output:

ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The new ArrayDeque is: []

Example 3: Using ArrayList Class




// 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);
    }
}


Output:

ArrayList: [15, 20, 25]
The new ArrayList is: []

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#clear–

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments