Java Utility Library

Java Vector - contains() Method



The java.util.Vector.contains() method is used to check whether the vector contains the specified element or not. It returns true if the vector contains the specified element, else returns false.

Syntax

public boolean contains(Object obj)

Parameters

obj Specify element whose presence in the vector need to be tested.

Return Value

Returns true if the vector contains the specified element, else returns false.

Exception

NA

Example:

In the example below, the java.util.Vector.contains() method is used to check the presence of specified element in the given vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector 
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);

    //checking the presence of elements
    for(int i = 5; i <= 20; i += 5) {
      if(MyVector.contains(i))
        System.out.println(i +" is present in MyVector.");
      else
        System.out.println(i +" is NOT present in MyVector.");
    }       
  }
}

The output of the above code will be:

5 is NOT present in MyVector.
10 is present in MyVector.
15 is NOT present in MyVector.
20 is present in MyVector.

❮ Java.util - Vector