Write a Java program to check whether a Vector collection is empty or not


The code you provided demonstrates how to check if a Vector is empty or not using the isEmpty method in Java. Here's a breakdown of what the code does:

  • Two Vector<String> objects named vec_list1 and vec_list2 are created.
  • vec_list2 is populated with elements "Apple", "Grapes", "Banana", "Papaya", "Cherry", and "Orange" using the add method.
  • The isEmpty method is used to check if vec_list1 is empty. If it is empty, the message "Vector List 1 is Empty Collection" is printed. Otherwise, the message "Vector List 1 is Not Empty Collection" is printed.
  • Similarly, the isEmpty method is used to check if vec_list2 is empty. If it is empty, the message "Vector List 2 is Empty Collection" is printed. Otherwise, the message "Vector List 2 is Not Empty Collection" is printed.

As you can see, vec_list1 is empty, so the first condition is true and the message "Vector List 1 is Empty Collection" is printed. On the other hand, vec_list2 is not empty, so the second condition is true and the message "Vector List 2 is Not Empty Collection" is printed.

Source Code

import java.util.*;
public class EmptyNot
{
	public static void main(String[] args)
	{
		Vector < String > vec_list1 = new Vector < String > ();
		Vector < String > vec_list2 = new Vector < String > ();
		vec_list2.add("Apple");
		vec_list2.add("Grapes");
		vec_list2.add("Banana");
		vec_list2.add("Papaya");
		vec_list2.add("Cherry");
		vec_list2.add("Orange");
 
		if (vec_list1.isEmpty())
		{
			System.out.println("Vector List 1 is Empty Collection");
		}
		else
		{
			System.out.println("Vector List 1 is Not Empty Collection");
		}
 
		if (vec_list2.isEmpty())
		{
			System.out.println("Vector List 2 is Empty Collection");
		}
		else
		{
			System.out.println("Vector List 2 is Not Empty Collection");
		}
	}
}

Output

Vector List 1 is Empty Collection
Vector List 2 is Not Empty Collection

Example Programs