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 Solution:

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.

Program/Source Code:

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();

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.

Preparation

What's New

Top Interview Coding Problems/Challenges!