Java Utility Library

Java Vector - removeAll() Method



The java.util.Vector.removeAll() method is used to remove from the given vector all of its elements that are contained in the specified collection.

Syntax

public boolean removeAll(Collection<?> c)

Parameters

c Specify the collection of elements to be removed from the vector.

Return Value

Returns true if the vector changed as a result of the call.

Exception

Throws NullPointerException, if the specified collection is null.

Example:

In the example below, the java.util.Vector.removeAll() method is used to remove from the given vector all of its elements that are contained in the specified collection.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> vec1 = new Vector<Integer>();
    Vector<Integer> vec2 = new Vector<Integer>();

    //populating vec1
    vec1.add(10);
    vec1.add(20);
    vec1.add(30);
    vec1.add(40);
    vec1.add(50);

    //populating vec2
    vec2.add(20);
    vec2.add(40);
    vec2.add(60);

    //printing vec1
    System.out.println("Before removeAll, vec1 contains: " + vec1);

    //apply removeAll method on vec1
    vec1.removeAll(vec2);

    //printing vec1
    System.out.println("After removeAll, vec1 contains: " + vec1); 
  }
}

The output of the above code will be:

Before removeAll, vec1 contains: [10, 20, 30, 40, 50]
After removeAll, vec1 contains: [10, 30, 50]

❮ Java.util - Vector