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

    vec1.add("CAR");
    vec1.add("BUS");
    vec1.add("BIKE");
    vec1.add("TRUCK");

    vec2.add("CAR");
    vec2.add("BUS");
    vec2.add("BIKE");
    vec2.add("TRUCK");

    vec3.add("CAR");
    vec3.add("BUS");
    vec3.add("BIKE");
    vec3.add("TRUCK");
    vec3.add("TRAIN");

    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.

Java Vector Class Programs »



Related Programs




Comments and Discussions!

Load comments ↻






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