Java Vector containsAll(Collection c) Example

We already know that contains(Object o) method of java.util.Vector class will help you in finding if the particular element is present or not whereas containsAll(Collection c) will return true if a Collection contains all the element of other Collection.

Signature

public boolean containsAll(Collection c)

This method returns true if the collection contains all of the elements in the specified collection.

Example

The following example shows the usage of java.util.Vector.containsAll(c) method.

import java.util.Vector;

public class ContainsAllMethodVectorExample {
    public static void main(String args[])
    {
        // create an empty Vector
        Vector vector1 = new Vector();
        
        // use add() method to add elements to the Vector
        vector1.add("Element1");
        vector1.add("Element2");
        vector1.add("Element3");
        vector1.add("Element4");
        vector1.add("Element5");
        vector1.add("Element6");
        
        Vector vector2 = new Vector();
        
        // use add() method to add elements to the Vector
        vector2.add("Element1");
        vector2.add("Element2");
        vector2.add("Element3");
        
        System.out.println("Does vector1 contains all the element of vector2 ?? "+vector1.containsAll(vector2));
        
        //Lets add a new element to vector2
        vector2.add("New Element");
        
        //Checking again
        System.out.println("Does vector1 contains all the element of vector2 ?? "+vector1.containsAll(vector2));
    }
}

Output

Does vector1 contains all the element of vector2 ?? true
Does vector1 contains all the element of vector2 ?? false

Leave a Reply

Your email address will not be published. Required fields are marked *