Java Vector retainAll() Method



Description

The Java Vector retainAll(Collection<?> c) method is used to retain only the elements in this Vector that are contained in the specified Collection. In other words, removes from this Vector all of its elements that are not contained in the specified Collection.

Declaration

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

public boolean retainAll(Collection<?> c)

Parameters

c − This is the collection of elements to be retained in this Vector.

Return Value

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

Exception

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

Retaining Elements of a Vector of Integer Example

The following example shows the usage of Java Vector retainAll(collection) method. We're creating an Vector of Integers, adding some elements, print it and then use retainAll(collection) method to retain 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 retain two common elements
      System.out.println("Vector modified:  " + vector.retainAll(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 = [20, 30]

Retaining Elements of a Vector of String Example

The following example shows the usage of Java Vector retainAll(collection) method. We're creating an Vector of Strings, adding some elements, print it and then use retainAll(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 vector
      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 retain two common elements
      System.out.println("Vector modified:  " + vector.retainAll(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 = [B, C]

Retaining Elements of a Vector of Object Example

The following example shows the usage of Java Vector retainAll(collection) method. We're creating an Vector of Student objects, adding some elements, print it and then use retainAll(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 vector
      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 retain two common elements
      System.out.println("Vector modified:  " + vector.retainAll(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 = [[ 1, Julie ], [ 3, Adam ]]
java_util_vector.htm
Advertisements