ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.