The Wayback Machine - https://web.archive.org/web/20240618020210/https://www.geeksforgeeks.org/vector-clear-method-in-java/
Open In App

Vector clear() Method in Java

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax:

Vector.clear()

Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Java.util.Vector.clear() method. Example 1: 

Java




// Java code to illustrate clear()
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vt = new Vector<String>();
 
        // Use add() method to add elements into the Vector
        vt.add("Welcome");
        vt.add("To");
        vt.add("Geeks");
        vt.add("4");
        vt.add("Geeks");
 
        // Displaying the Vector
        System.out.println("Vector: " + vt);
 
        // Clearing the Vector using clear() method
        vt.clear();
 
        // Displaying the final vector after clearing;
        System.out.println("The final Vector: " + vt);
    }
}


Output:

Vector: [Welcome, To, Geeks, 4, Geeks]
The final Vector: []

Example 2: 

Java




// Java code to illustrate clear()
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<Integer> vt = new Vector<Integer>();
 
        // Use add() method to add elements into the Queue
        vt.add(10);
        vt.add(15);
        vt.add(30);
        vt.add(20);
        vt.add(5);
 
        // Displaying the Vector
        System.out.println("Vector: " + vt);
 
        // Clearing the Vector using clear() method
        vt.clear();
 
        // Displaying the final vector after clearing;
        System.out.println("The final Vector: " + vt);
    }
}


Output:

Vector: [10, 15, 30, 20, 5]
The final Vector: []

Time complexity: O(n). // n is the size of the vector.

Auxiliary Space: O(n).



Previous Article
Next Article

Similar Reads

Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Java.util.BitSet.clear() in Java
There are three variants of clear() method: clear() : The clear() method sets all of the bits in this BitSet to false. public void clear() Return Value This method does not return a value. // Java code to demonstrate the working // of clear() in BitSet import java.util.*; public class BitClr1 { public static void main(String[] args) { // Declaring
3 min read
HashSet clear() Method in Java
The Java.util.HashSet.clear() method is used to remove all the elements from a HashSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing HashSet. Syntax: Hash_Set.clear() Parameters: The method does not take any parameter R
1 min read
ArrayList clear() Method in Java with Examples
The clear() method of ArrayList in Java is used to remove all the elements from a list. The list will be empty after this call returns so whenever this operation has been performed all elements of the corresponding ArrayList will be deleted so it does it becomes an essential function for deleting elements in ArrayList from memory leading to optimiz
2 min read
LinkedBlockingDeque clear() method in Java
The clear() method of LinkedBlockingDeque erases all the elements that are present in the LinkedBlockingDeque container. The container becomes empty after the function is called. Syntax: public void clear() Parameters: This method does not accepts any parameters Returns: This method does not returns anything. Below programs illustrate clear() metho
2 min read
LinkedList clear() Method in Java
The Java.util.LinkedList.clear() method is used to remove all the elements from a linked list. Using the clear() method only clears all the element from the list and not deletes the list. In other words we can say that the clear() method is used to only empty an existing LinkedList. Syntax: void clear() Parameters: This function does not accept any
1 min read
TreeSet clear() Method in Java
The Java.util.TreeSet.clear() method is used to remove all the elements from a TreeSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing TreeSet. Syntax: Tree_Set.clear() Parameters: The method does not take any parameter R
1 min read
PriorityQueue clear() Method in Java
The Java.util.PriorityQueue.clear() method is used to remove all the elements from a PriorityQueue. Using the clear() method only clears all the element from the queue and does not delete the queue. In other words, we can say that the clear() method is used to only empty an existing PriorityQueue. Syntax: Priority_Queue.clear() Parameters: The meth
2 min read
HashMap clear() Method in Java
The java.util.HashMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified HashMap. Syntax: Hash_Map.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the working of java.util.HashMap.clear() Method:
2 min read
LinkedHashMap clear() Method in Java
The java.util.LinkedHashMap.clear() is an inbuilt method of LinkedHashMap class in Java and is used to clear all the elements or the mappings from the denoted LinkedHashMap. The map will become empty after the operation is performed. Syntax: Linked_Hash_Map.clear() Parameters: The method does not take any parameters. Return Value: The method does n
2 min read