The containsAll
method checks if all the elements of the specific collection object are present in the Vector
object.
The
Vector
class is a growable array of objects. The elements ofVector
can be accessed using an integer index and the size of aVector
can be increased or decreased. Read more aboutVector
here.
public boolean containsAll(Collection<?> c)
This method takes the collection to be checked if it is present in the vector
object.
It will return true
if all the elements of the passed collection are present in the Vector
object.
Please click the “Run” button below to see how the
containsAll
method works.
import java.util.Vector;import java.util.ArrayList;class containsAll {public static void main( String args[] ) {Vector<Integer> vector = new Vector<>();vector.add(1);vector.add(2);vector.add(3);ArrayList<Integer> list1 = new ArrayList<>();list1.add(1);list1.add(3);System.out.println("The vector is "+ vector);System.out.println("\nlist1 is "+ list1);System.out.println("If vector contains all elements of list1 : "+ vector.containsAll(list1));ArrayList<Integer> list2 = new ArrayList<>();list2.add(4);System.out.println("\nlist2 is "+ list2);System.out.println("If vector contains all elements of list2 : "+ vector.containsAll(list2));}}
In the above code:
Line 1 and 2: We imported the Vector
and ArrayList
class
Line 5: We created an object for the Vector
class with the name vector
.
Lines 6 - 8: We added three elements (1,2,3
) to the above created vector object.
Line 10: We created a new ArrayList
object with the name list1
.
Line 11 and 12: We added the elements 1,3
to list1
.
Line 16: We used the containsAll
method to check if all the elements of list1
are present in the vector
. In this case, true
is returned. This is because all the elements (1,3
) of list1
are present in the vector
.
Line 18: We created a new ArrayList
object with the name list2
.
Line 19: We added an element 4
to list2
.
Line 22: We used the conainsAll
method to check if all the elements of the list2
are present in the vector
. In this case, false
is returned. This is because element 4
of the list2
is not present in the vector
.