# Java program to compare two Vector collections

Given two Vector collections, we have to compare them.
Submitted by Nidhi, on May 21, 2022

Problem Solution:

In this program, we will create 3 Vector collections with string elements. Then we will compare vector collections using the equals() method.

Program/Source Code:

The source code to compare two Vector collections is given below. The given program is compiled and executed successfully.

```// Java program to compare two Vector elements

import java.util.*;

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

if (vec1.equals(vec2))
System.out.println("Vectors vec1 and vec2 have similar elements.");
else
System.out.println("Vectors vec1 and vec2 does not have similar elements.");

if (vec1.equals(vec3))
System.out.println("Vectors vec1 and vec3 have similar elements.");
else
System.out.println("Vectors vec1 and vec3 does not have similar elements.");
}
}
```

Output:

```Vectors vec1 and vec2 have similar elements.
Vectors vec1 and vec3 does not have similar elements.
```

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 Vector collections vec and add elements to it. Then we compared Vector collections using the equals() method and printed appropriate messages.

Preparation

What's New

Top Interview Coding Problems/Challenges!