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 code to illustrate removeAll() import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<String> vec_tor = new Vector<String>(); // Use add() method to add elements in the Vector vec_tor.add("Geeks"); vec_tor.add("for"); vec_tor.add("Geeks"); vec_tor.add("10"); vec_tor.add("20"); // Output the Vector System.out.println("Vector: " + vec_tor); // Creating an empty Vector Vector<String> colvec_tor = new Vector<String>(); // Use add() method to add elements in the Vector colvec_tor.add("Geeks"); colvec_tor.add("for"); colvec_tor.add("Geeks"); // Remove the head using remove() boolean changed = vec_tor.removeAll(colvec_tor); // Print the result if (changed) System.out.println("Collection removed"); else System.out.println("Collection not removed"); // Print the final Vector System.out.println("Final Vector: " + vec_tor); } } |
Vector: [Geeks, for, Geeks, 10, 20] Collection removed Final Vector: [10, 20]
Program 2:
// Java code to illustrate removeAll() import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<Integer> vec_tor = new Vector<Integer>(); // Use add() method to add elements in the Vector vec_tor.add(1); vec_tor.add(2); vec_tor.add(3); vec_tor.add(10); vec_tor.add(20); // Output the Vector System.out.println("Vector: " + vec_tor); // Creating an empty Vector Vector<Integer> colvec_tor = new Vector<Integer>(); // Use add() method to add elements in the Vector colvec_tor.add(30); colvec_tor.add(40); colvec_tor.add(50); // Remove the head using remove() boolean changed = vec_tor.removeAll(colvec_tor); // Print the result if (changed) System.out.println("Collection removed"); else System.out.println("Collection not removed"); // Print the final Vector System.out.println("Final Vector: " + vec_tor); } } |
Vector: [1, 2, 3, 10, 20] Collection not removed Final Vector: [1, 2, 3, 10, 20]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ArrayDeque removeAll() method in Java
- ConcurrentSkipListSet removeAll() method in Java
- ArrayList removeAll() method in Java with Examples
- AbstractSet removeAll() Method in Java with Examples
- CopyOnWriteArrayList removeAll() method in Java with Examples
- AbstractSequentialList removeAll() method in Java with Example
- LinkedHashSet removeAll() method in Java with Example
- Stack removeAll() method in Java with Example
- HashSet removeAll() method in Java with Example
- AbstractCollection removeAll() method in Java with Example
- TreeSet removeAll() method in Java with Example
- Set removeAll() method in Java with Examples
- List removeAll() method in Java with Examples
- CopyOnWriteArraySet removeAll() method in Java with Examples
- LinkedTransferQueue removeAll() method in Java with Examples
- LinkedBlockingDeque removeAll() method in Java with Examples
- SortedSet removeAll() method in Java with Examples
- Vector clear() Method in Java
- Vector addAll() Method in Java
- Vector set() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

