A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/vector-retainall-method-in-java-with-examples/ below:

Vector retainAll() method in Java with Examples

Vector retainAll() method in Java with Examples

Last Updated : 11 Jul, 2025

The

retainAll

method of class

vector in java

is an inbuilt function in Java which is used to retains 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.

Syntax:
public boolean retainAll(Collection c)
Parameter:

Here

c

is a collection of elements to be retained in this Vector (all other elements are removed).

Return value:

This method will return a

boolean value

, i.e true if this Vector changed as a result of the call or else false.

Exception:

This method will throw the following exceptions.

  • ClassCastException - If the types of one or more elements in this vector are incompatible with the specified collection.
  • NullPointerException -If this vector contains one or more null elements and the specified collection does not support null elements, or if the specified collection is null.
  • Below programs illustrate the Vector.retainAll() method in Java:

    Program 1:

    To illustrate Vector.retainAll() method in Java.

    Java
    import java.util.*;
    import java.io.*;
    public class GFG {
        public static void main(String args[])
        {
            // Creating an empty Vector
            Vector<String> v1 = new Vector<String>();
    
            // adding elements to the vector v1
            v1.add("Geeks");
            v1.add("For");
            v1.add("Geeks");
            v1.add("is");
            v1.add("a");
            v1.add("computer");
            v1.add("science");
            v1.add("portal");
    
            System.out.println("Elements of vector1:" + v1);
    
            // Creating an other empty vector
            Vector<String> v2 = new Vector<String>();
    
            // adding elements to the vector v2
            v2.add("Geeks");
            v2.add("For");
            v2.add("Geeks");
            v2.add("contains");
            v2.add("well");
            v2.add("written");
            v2.add("programming");
            v2.add("articles");
            v2.add("and");
            v2.add("much");
            v2.add("more.");
    
            System.out.println("Elements of vector2:" + v2);
    
            System.out.println("Calling retainAll() method");
            // calling retainAll()
            v1.retainAll(v2);
    
            System.out.println("After calling retainAll() method");
            System.out.println(v1);
        }
    }
    
    Output:
    Elements of vector1:[Geeks, For, Geeks, is, a, computer, science, portal] Elements of vector2:[Geeks, For, Geeks, contains, well, written, programming, articles, and, much, more.] Calling retainAll() method After calling retainAll() method [Geeks, For, Geeks]
    Program 2:

    To show return value of retainAll() method in Java.

    Java
    import java.util.*;
    import java.io.*;
    
    public class GFG {
        public static void main(String args[])
        {
    
            // Creating an empty Vector
            Vector<String> v1 = new Vector<String>();
    
            // adding elements to the vector v1
            v1.add("Geeks");
            v1.add("For");
            v1.add("Geeks");
            v1.add("is");
            v1.add("a");
            v1.add("computer");
            v1.add("science");
            v1.add("portal");
    
            System.out.println("Elements of vector1:" + v1);
    
            // Creating an other empty vector
            Vector<String> v2 = new Vector<String>();
    
            // adding elements to the vector v2
            v2.add("Geeks");
            v2.add("For");
            v2.add("Geeks");
            v2.add("contains");
            v2.add("well");
            v2.add("written");
            v2.add("programming");
            v2.add("articles");
            v2.add("and");
            v2.add("many");
            v2.add("more.");
    
            System.out.println("Elements of vector1:" + v1);
    
            // calling retainAll()
            boolean t = v1.retainAll(v2);
    
            System.out.println("Calling retainAll() method: ");
            System.out.println(t);
        }
    }
    
    Output:
    Elements of vector1:[Geeks, For, Geeks, is, a, computer, science, portal] Elements of vector1:[Geeks, For, Geeks, is, a, computer, science, portal] Calling retainAll() method: true


    RetroSearch is an open source project built by @garambo | Open a GitHub Issue

    Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

    HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4