Open In App
Related Articles

Vector containsAll() Method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
The java.util.vector.containsAll() method is used to check to check if this Vector contains all of the elements in the specified Collection. So basically it is used to check if a vector contains a set of elements or not. Syntax:
Vector.containsAll(Collection col)
Parameters: This method accepts a mandatory parameter col which is of the type of vector. This is the collection whose elements are needed to be checked if it is present in the vector or not. Return Value: The method returns True if all elements in the collection col are present in the vector otherwise it returns False. Exception: The method throws NullPointerException if the specified collection is NULL. Below programs illustrates the Java.util.Vector.containsAll() method: Program 1:
// Java code to illustrate containsAll()
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 into the Vector
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Creating another empty Vector
        Vector<String> colvec_tor = new Vector<String>();
  
        colvec_tor.add("Geeks");
        colvec_tor.add("4");
        colvec_tor.add("Geeks");
  
        System.out.println("Are all the contents equal? " + vec_tor.containsAll(colvec_tor));
  
        // Creating another empty Vector
        Vector<String> colvec_tor2 = new Vector<String>();
  
        colvec_tor2.add("Hello");
        colvec_tor2.add("Geeks");
  
        System.out.println("Are all the contents equal? " + vec_tor.containsAll(colvec_tor2));
    }
}

                    
Output:
Vector: [Welcome, To, Geeks, 4, Geeks]
Are all the contents equal? true
Are all the contents equal? false
Program 2:
// Java code to illustrate contains()
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 into the Vector
        vec_tor.add(10);
        vec_tor.add(15);
        vec_tor.add(30);
        vec_tor.add(20);
        vec_tor.add(5);
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Creating another empty Vector
        Vector<Integer> colvec_tor = new Vector<Integer>();
  
        colvec_tor.add(20);
        colvec_tor.add(25);
        colvec_tor.add(30);
  
        System.out.println("Are all the contents equal? " + vec_tor.containsAll(colvec_tor));
  
        // Creating another empty Vector
        Vector<Integer> colvec_tor2 = new Vector<Integer>();
  
        colvec_tor2.add(10);
        colvec_tor2.add(20);
  
        System.out.println("Are all the contents equal? " + vec_tor.containsAll(colvec_tor2));
    }
}

                    
Output:
Vector: [10, 15, 30, 20, 5]
Vector: [10, 15, 30, 20, 5]
Are all the contents equal? false
Are all the contents equal? true


Last Updated : 17 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads