Java Vector contains() containsAll() Methods Tutorial

In this section we will learn what the Vector contains(), containsAll(), methods are and how to use them in Java.

What is Java Vector contains() Method?

The Java Vector contains() method is used to check a vector object and see if it has a specific element or not.

We put this element as the argument of this method and it will return either true or false to tell us whether the element is in the target vector object.

Java Vector contains() Method Syntax:

public boolean contains(Object o)

contains() Method Parameters:

The method takes one argument and that is the element we want to search for it in the target vector object.

contains() Method Return Value:

The return value of this method is of type Boolean.

If the target vector object had the element, the return value of this method becomes true otherwise the value false will return.

contains() Method Exceptions:

The method does not throw an exception.

Example: using Vector contains() method

import java.util.Vector; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        System.out.println(vector.contains("Ellen"));
        System.out.println(vector.contains("Rayan"));
    }
}

Output:

true

false

What is Java Vector containsAll() Method?

The Java Vector containsAll() method is used to check a vector object with the elements of a collection object.

Basically, using this method, we can see if the entire elements of a collection is in a vector object. If a vector object had the entire elements of the target collection, then the return value of this method will be true. Otherwise, we get the value false.

Java Vector containsAll() Method Syntax:

public boolean containsAll(Collection<?> c)

containsAll() Method Parameters:

The method takes on argument, and that is a reference to a collection we want to search its elements in the target vector object.

Note: the collection object should’ve implemented the Collection interface.

containsAll() Method Return Value:

The return value of this method is of type Boolean.

If the entire elements of the target collection (the one that we’ve passed as the argument of the method) was in fact in the target vector object, then the return value of this method will be true, otherwise even if one element was missing from the target vector object, the value false will return instead.

containsAll() Method Exceptions:

The method might throw one exception:

NullPointerException: We get this exception if we put a null value as the argument of this method.

Example: using Vector containsAll() method

import java.util.Vector; 
import java.util.ArrayList;
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        ArrayList<String> list = new ArrayList<>();
        list.add("Omid");
        list.add("Jack");

        System.out.println(vector.containsAll(list));
    }
}

Output:

true

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies