Java Vector containsAll() Method



Description

The Java Vector containsAll(Collection<?> c) method is used to check if this Vector contains all of the elements in the specified Collection.

Declaration

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

public boolean containsAll(Collection<?> c)

Parameters

c − The input parameter is a collection whose elements will be tested for containment in this Vector.

Return Value

true − Returns true if this Vector contains all of the elements in the specified collection.

Exception

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

Checking Elements' Presence in a Vector of Integer Example

The following example shows the usage of Java Vector containsAll() method to check multiples elements presence in single go. We're creating three vector objects of Integer. Then elements are added to each object. Two vector contains same elements and one is having different elements. Then using containsAll() method we're comparing these vectors and printing the status.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create three empty Vectors vec and vectest with an initial capacity of 4      
      Vector<Integer> vec = new Vector<>(4);
      Vector<Integer> vectest = new Vector<>(4);
      Vector<Integer> vecdiff = new Vector<>(4);

      // use add() method to add elements in the vector vec
      vec.add(4);
      vec.add(3);
      vec.add(2);   

      // use add() method to add elements in the vector vectest
      vectest.add(4);
      vectest.add(3);
      vectest.add(2);

      // use add() method to add elements in the vector vecdiff
      vecdiff.add(4);
      vecdiff.add(3);
      vecdiff.add(12);

      // let us check vec and vectest
      System.out.println("Checking contents in vec and vectest : "+vectest.containsAll(vec));   

      // let us check vec and vecdiff
      System.out.println("Checking contents in vec and vecdiff : "+vecdiff.containsAll(vec)); 
   }
}

Output

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

Checking contents in vec and vectest : true
Checking contents in vec and vecdiff : false

Checking Elements' Presence in a Vector of String Example

The following example shows the usage of Java Vector containsAll() method to check multiples elements presence in single go. We're creating three vector objects of String. Then elements are added to each object. Two vector contains same elements and one is having different elements. Then using containsAll() method we're comparing these vectors and printing the status.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create three empty Vectors vec and vectest with an initial capacity of 4      
      Vector<String> vec = new Vector<>(4);
      Vector<String> vectest = new Vector<>(4);
      Vector<String> vecdiff = new Vector<>(4);

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

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

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

      // let us check vec and vectest
      System.out.println("Checking contents in vec and vectest : "+vectest.containsAll(vec));   

      // let us check vec and vecdiff
      System.out.println("Checking contents in vec and vecdiff : "+vecdiff.containsAll(vec)); 
   }
}

Output

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

Checking contents in vec and vectest : true
Checking contents in vec and vecdiff : false

Checking Elements' Presence in a Vector of Object Example

The following example shows the usage of Java Vector containsAll() method to check multiples elements presence in single go. We're creating three vector objects of Student objects. Then elements are added to each object. Two vector contains same elements and one is having different elements. Then using containsAll() method we're comparing these vectors and printing the status.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create three empty Vectors vec and vectest with an initial capacity of 4      
      Vector<Student> vec = new Vector<>(4);
      Vector<Student> vectest = new Vector<>(4);
      Vector<Student> vecdiff = new Vector<>(4);

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

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

      // use add() method to add elements in the vector vecdiff
      vecdiff.add(new Student(1, "Julie"));
      vecdiff.add(new Student(2, "Robert"));
      vecdiff.add(new Student(4, "Jane")); 

      // let us check vec and vectest
      System.out.println("Checking contents in vec and vectest : "+vectest.containsAll(vec));   

      // let us check vec and vecdiff
      System.out.println("Checking contents in vec and vecdiff : "+vecdiff.containsAll(vec)); 
   }
}
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.

Checking contents in vec and vectest : true
Checking contents in vec and vecdiff : false
java_util_vector.htm
Advertisements