The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified.
Syntax:
Vector.removeAll(Collection col)
Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from the vector.
Return Value: This method returns true if the vector is altered due to the operation at all, else False. Exception: The method throws NullPointerException if the specified collection is null.
Below programs illustrate the Java.util.Vector.removeAll(Collection col) method:
Program 1:
Java
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
Vector<String> vec_tor = new Vector<String>();
vec_tor.add("Geeks");
vec_tor.add(" for ");
vec_tor.add("Geeks");
vec_tor.add(" 10 ");
vec_tor.add(" 20 ");
System.out.println("Vector: " + vec_tor);
Vector<String> colvec_tor = new Vector<String>();
colvec_tor.add("Geeks");
colvec_tor.add(" for ");
colvec_tor.add("Geeks");
boolean changed = vec_tor.removeAll(colvec_tor);
if (changed)
System.out.println("Collection removed");
else
System.out.println("Collection not removed");
System.out.println("Final Vector: " + vec_tor);
}
}
|
Output:
Vector: [Geeks, for, Geeks, 10, 20]
Collection removed
Final Vector: [10, 20]
Program 2:
Java
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
Vector<Integer> vec_tor = new Vector<Integer>();
vec_tor.add( 1 );
vec_tor.add( 2 );
vec_tor.add( 3 );
vec_tor.add( 10 );
vec_tor.add( 20 );
System.out.println("Vector: " + vec_tor);
Vector<Integer> colvec_tor = new Vector<Integer>();
colvec_tor.add( 30 );
colvec_tor.add( 40 );
colvec_tor.add( 50 );
boolean changed = vec_tor.removeAll(colvec_tor);
if (changed)
System.out.println("Collection removed");
else
System.out.println("Collection not removed");
System.out.println("Final Vector: " + vec_tor);
}
}
|
Output:
Vector: [1, 2, 3, 10, 20]
Collection not removed
Final Vector: [1, 2, 3, 10, 20]
Time complexity: O(n^2). // n is the size of the vector.
Auxiliary Space: O(n).
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 May, 2023
Like Article
Save Article