Write a Java program to check whether a Vector collection contains a specified item or not


The code you provided demonstrates how to check if a specific item exists in a vector using the contains method in Java. Here's a breakdown of what the code does:

  • A Vector object named vec_list is created.
  • Elements "Apple", "Banana", "Cherry", and "Mango" are added to vec_list using the add method.
  • The contains method is used to check if the vector vec_list contains the item "Cherry". If it does, the message "Vector vec_list contains item 'Cherry'." is printed. Otherwise, the message "Vector vec_list does not contain item 'Cherry'." is printed.

Since the vector vec_list contains the item "Cherry", the contains method returns true, and the corresponding message is printed.

Source Code

import java.util.*;
public class Check_ItemNot
{
	public static void main(String[] args)
	{
		Vector vec_list = new Vector();
		vec_list.add("Apple");
		vec_list.add("Banana");
		vec_list.add("Cherry");
		vec_list.add("Mango");
 
		if (vec_list.contains("Cherry"))
			System.out.println("Vector vec_list contains item 'Cherry'.");
		else
			System.out.println("Vector vec_list does not contain item 'Cherry'.");
	}
}

Output

Vector vec_list contains item 'Cherry'.

Example Programs