Java Vector removeAll() Method



Description

The Java Vector removeAll(Collection<?> c) is used to remove all the elements of the vector that are contained in the specified Collection.

Declaration

Following is the declaration for java.util.Vector.removeAll() method

public boolean removeAll(Collection<?> c)

Parameters

c − This is a collection of elements to be removed from the Vector

Return Value

The method call returns true if this Vector changed as a result of the call.

Exception

NullPointerException − This exception is thrown if the specified collection is null.

Remove multiple Elements of a Vector of Integer Example

The following example shows the usage of Java Vector removeAll(collection) method. We're creating an Vector of Integers, adding some elements, print it and then use removeAll(collection) method to remove few elements. As Vector is modified it is printed to check if specified elements are removed or not.

package com.tutorialspoint;

import java.util.Vector;
import java.util.Arrays;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Integer> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(20);
      vector.add(15);
      vector.add(30);
      vector.add(45);

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // it will remove two common elements
      System.out.println("Vector modified:  " + vector.removeAll(Arrays.asList(11,30,20,12)));
	  
      // let us print all the elements available in vector again
      System.out.println("Vector = " + vector);
   } 	
}   

Output

Let us compile and run the above program, this will produce the following result −

Vector = [20, 15, 30, 45]
Vector modified:  true
Vector = [15, 45]

Remove multiple Elements of a Vector of String Example

The following example shows the usage of Java Vector removeAll(collection) method. We're creating an Vector of Strings, adding some elements, print it and then use removeAll(collection) method to remove few elements. As Vector is modified it is printed to check if specified elements are removed or not.

package com.tutorialspoint;

import java.util.Vector;
import java.util.Arrays;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<String> vector = new Vector<>();

      // use add() method to add elements in the deque
      vector.add("A");
      vector.add("B");
      vector.add("C");
      vector.add("D");

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // it will remove two common elements
      System.out.println("Vector modified:  " + vector.removeAll(Arrays.asList("B","C","E")));
	  
      // let us print all the elements available in vector again
      System.out.println("Vector = " + vector);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Vector = [A, B, C, D]
Vector modified:  true
Vector = [A, D]

Remove multiple Elements of a Vector of Object Example

The following example shows the usage of Java Vector removeAll(collection) method. We're creating an Vector of Student objects, adding some elements, print it and then use removeAll(collection) method to remove few elements. As Vector is modified it is printed to check if specified elements are removed or not.

package com.tutorialspoint;

import java.util.Vector;
import java.util.Arrays;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Student> vector = new Vector<>();

      // use add() method to add elements in the deque
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.add(new Student(3, "Adam"));
      vector.add(new Student(4, "Jene"));

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // it will remove two common elements
      System.out.println("Vector modified:  " + vector.removeAll(Arrays.asList(new Student(1, "Julie"),new Student(3, "Adam"))));
	  
      // let us print all the elements available in vector again
      System.out.println("Vector = " + vector);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ]]
Vector modified:  true
Vector = [[ 2, Robert ], [ 4, Jene ]]
java_util_vector.htm
Advertisements