×

Java Programs

Java Practice

Java program to check whether a Vector collection contains all elements of the specified collection or not

Given a Vector collection, we have to check whether a it contains all elements of the specified collection or not.
Submitted by Nidhi, on May 20, 2022

Problem statement

In this program, we will create a Vector collection with a different types of elements. Then we will check whether a vector collection contains all elements of the specified collection or not using the containsAll() method. The containsAll() method returns true if a vector collection contains all elements of the specified collection otherwise it returns false.

Java program to check whether a Vector collection contains all elements of the specified collection or not

The source code to check whether a Vector collection contains all elements of the specified collection or not is given below. The given program is compiled and executed successfully.

// Java program to check a Vector collection contains 
// all elements of the specified collection

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Vector vec1 = new Vector();
    Vector vec2 = new Vector();
    Vector vec3 = new Vector();

    vec1.add(10);
    vec1.add(20);
    vec1.add(30);
    vec1.add(20.5);
    vec1.add(true);
    vec1.add("Hello World");

    vec2.add("A");
    vec2.add("B");
    vec2.add("C");
    vec2.add("D");

    vec3.add(10);
    vec3.add(20.5);
    vec3.add(true);
    vec3.add("Hello World");

    if (vec1.containsAll(vec2))
      System.out.println("Vector vec1 contains all elements of vec2.");
    else
      System.out.println("Vector vec1 does not contain all elements of vec2.");

    if (vec1.containsAll(vec3))
      System.out.println("Vector vec1 contains all elements of vec3.");
    else
      System.out.println("Vector vec1 does not contain all elements of vec3.");
  }
}

Output

Vector vec1 does not contain all elements of vec2.
Vector vec1 contains all elements of vec3.

Explanation

In the above program, we imported the "java.util.*" package to use the Vector class. Here, we created a public class Main.

The Main class contains a main() method. The main() method is the entry point for the program. And, created a Vector collection vec and add elements to it. Then we checked whether a vector contains all elements of the specified collection or not using the containsAll() method and printed the appropriate message.

Java Vector Class Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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