Write a Java program to remove all elements of Vector collection contained in the specified collection


The code you provided demonstrates how to remove all elements from one Vector that are also present in another Vector using the removeAll method in Java. Here's a breakdown of what the code does:

  • Two Vector objects named vec_list1 and vec_list2 are created.
  • Elements "Apple", "Banana", "Orange", "Cherry", and "Mango" are added to vec_list1 using the add method.
  • Elements "Papaya", "Apple", "Grapes", and "Cherry" are added to vec_list2 using the add method.
  • The initial contents of vec_list1 and vec_list2 are printed using System.out.println.
  • The removeAll method is used on vec_list1, passing vec_list2 as an argument. This removes all elements from vec_list1 that are also present in vec_list2.
  • The updated contents of vec_list1 and vec_list2 are printed using System.out.println.

As you can see, after calling vec_list1.removeAll(vec_list2) , the elements "Apple" and "Cherry" are removed from vec_list1 because they are present in vec_list2. The resulting vec_list1 contains only the elements that were not present in vec_list2. vec_list2 remains unchanged.

Source Code

import java.util.*;
public class Remove_AllElements
{
	public static void main(String[] args)
	{
		Vector vec_list1 = new Vector();
		Vector vec_list2 = new Vector();
 
		vec_list1.add("Apple");
		vec_list1.add("Banana");
		vec_list1.add("Orange");
		vec_list1.add("Cherry");
		vec_list1.add("Mango");
 
		vec_list2.add("Papaya");
		vec_list2.add("Apple");
		vec_list2.add("Grapes");
		vec_list2.add("Cherry");
 
		System.out.println("Vector List 1 : " + vec_list1);
		System.out.println("Vector List 2 : " + vec_list2);
 
		vec_list1.removeAll(vec_list2);
 
		System.out.println("Vector List 1 : " + vec_list1);
		System.out.println("Vector List 2 : " + vec_list2);
	}
}

Output

Vector List 1 : [Apple, Banana, Orange, Cherry, Mango]
Vector List 2 : [Papaya, Apple, Grapes, Cherry]
Vector List 1 : [Banana, Orange, Mango]
Vector List 2 : [Papaya, Apple, Grapes, Cherry]

Example Programs