Home » Java programming language

Java Vector containsAll() Method with Example

Vector Class containsAll() method: Here, we are going to learn about the containsAll() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 15, 2020

Vector Class containsAll() method

  • containsAll() method is available in java.util package.
  • containsAll() method is used to test the existence of all the elements of this Vector in the given Collection (co).
  • containsAll() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • containsAll() method may throw an exception at the time of checking the existence of the collection.
    NullPointerException: This exception may throw when the given parameter is null exists.

Syntax:

    public boolean containsAll(Collection co);

Parameter(s):

  • Collection co – represents the Collection object whose element will be checked for the existence in this Vector.

Return value:

The return type of the method is boolean, it returns true when this Vector hold all of the given Collection elements otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean containsAll(Collection co) method 
// of Vector 

import java.util.*;

public class ContainsAllOfVector {
    public static void main(String[] args) {
        // Instantiates a Vector object  with
        // initial capacity of "10"
        Vector < String > v = new Vector < String > (10);
        ArrayList arr_l = new ArrayList();

        // By using add() method is to add the
        // elements in this v
        v.add("C");
        v.add("C++");
        v.add("JAVA");

        // By using add() method is to add the
        // elements in this arr_l
        arr_l.add("C");
        arr_l.add("C++");
        arr_l.add("SFDC");

        // Display Vector and ArrayList
        System.out.println("v: " + v);
        System.out.println("arr_l: " + arr_l);

        // By using containsAll(arr_l) method is to
        // check whether this vector contains all of the
        // elements in the given collection arr_l or not
        boolean status = v.containsAll(arr_l);

        // Display status
        System.out.println("v.containsAll(arr_l): " + status);
    }
}

Output

v: [C, C++, JAVA]
arr_l: [C, C++, SFDC]
v.containsAll(arr_l): false



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.