Java Vector contains() Method Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The contains() method in Java is used to check whether a specific element is present in the Vector or not.Example 1: In this example, we will check whether a particular string is present in the vector or not. Java // Java program to demonstrate the use // of the contains() method with Strings import java.util.Vector; public class Geeks { public static void main(String[] args) { // Create a vector to store strings Vector<String> v = new Vector<>(); // Add elements to the vector v.add("Welcome"); v.add("To"); v.add("Geeks"); v.add("4"); v.add("Geeks"); // Display the contents of the vector System.out.println("Vector: " + v); // Check if the vector contains "Geeks" System.out.println("Contains 'Geeks'? " + v.contains("Geeks")); // Check if the vector contains "4" System.out.println("Contains '4'? " + v.contains("4")); // Check if the vector contains "No" System.out.println("Contains 'No'? " + v.contains("No")); } } OutputVector: [Welcome, To, Geeks, 4, Geeks] Contains 'Geeks'? true Contains '4'? true Contains 'No'? false Syntax of Vector contains() Methodboolean contains(Object obj)Parameters: obj: The element to be checked for its presence in vector.Return Type: It returns true, if the element is present in the vector It returns false, if the element is not present in the vector Example 2: In this example, we will check whether a particular integer element is present in the vector or not. Java // Java program to demonstrate the use // of the contains() method with Integers import java.util.Vector; public class Geeks { public static void main(String[] args) { // Create a vector to store integers Vector<Integer> v = new Vector<>(); // Add elements to the vector v.add(10); v.add(15); v.add(30); v.add(20); v.add(5); // Display the contents of the vector System.out.println("Vector: " + v); // Check if the vector contains 100 System.out.println("Contains 100? " + v.contains(100)); // Check if the vector contains 30 System.out.println("Contains 30? " + v.contains(30)); } } OutputVector: [10, 15, 30, 20, 5] Contains 100? false Contains 30? true Example 3: The below Java program demonstrates how the contains() method works with custom objects by overriding the equals() method. Java // Java program to demonstrate the // contains() method with custom objects import java.util.Vector; class Person { String name; int age; // Constructor to initialize Person objects Person(String name, int age) { this.name = name; this.age = age; } // Overriding equals() to compare custom objects @Override public boolean equals(Object obj) { // Check if the same object is compared if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) // Check for null or different types return false; Person person = (Person) obj; return age == person.age && name.equals(person.name); } // Overriding toString() for // better output readability @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } } public class Main { public static void main(String[] args) { // Create a vector to store custom Person objects Vector<Person> v = new Vector<>(); // Add Person objects to the vector v.add(new Person("Geek1", 25)); v.add(new Person("Geek2", 30)); v.add(new Person("Geek3", 35)); // Display the contents of the vector System.out.println("Vector: " + v); // Check if the vector contains // a specific Person object System.out.println("Contains Geek1? " + v.contains(new Person("Geek1", 25))); // Check if the vector contains // another specific Person object System.out.println("Contains Geek5? " + v.contains(new Person("Geek5", 40))); } } OutputVector: [Person{name='Geek1', age=25}, Person{name='Geek2', age=30}, Person{name='Geek3', age=35}] Contains Geek1? true Contains Geek5? false Comment More infoAdvertise with us Next Article Java Vector contains() Method K kundankumarjha Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-Vector +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Vector Class in Java The Vector class in Java implements a growable array of objects. Vectors were legacy classes, but now it is fully compatible with collections. It comes under java.util package and implement the List interface.Key Features of Vector:It expands as elements are added.Vector class is synchronized in nat 12 min read Vector add() Method in Java To add an element to a Vector in Java, you can use the add() method, which appends an element to the end of the vector.Implementation:Java// Java code to illustrate Adding // Element in Vector import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Vector 3 min read Vector set() Method in Java The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element. Syntax: Vector.set(int index, Object element) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below. 2 min read Vector remove() Method in Java remove(int index) The java.util.vector.remove(int index) method is used to remove an element from a Vector from a specific position or index. Syntax: Vector.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element 2 min read Vector get() Method in Java The java.util.vector.get() method is used to fetch or retrieve an element at a specific index from a Vector. Syntax: Vector.get(int index) Parameters: This method accepts a mandatory parameter index which is of integer data type. It specifies the position or index of the element to be fetched from t 2 min read Vector addAll() Method in Java In Java, the addAll() method is used to add all elements from a specified Collection to the current Vector. The addAll() method allows you to efficiently append multiple elements to a vector in one operation.Implementation:Java// Java Program to addAll elements // From Vector to Vector import java.u 3 min read Vector addElement() Method in Java The Java.util.Vector.addElement() method is used to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of Vector class.Syntax: boolean addElement(Object element)Parameters: This funct 2 min read Vector capacity() Method in Java The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector.Syntax of Vector capacity() methodVector.capacity()Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the intern 2 min read Vector clear() Method in Java The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax:Vector.clear( 2 min read Java Vector clone() Method In Java, the clone() method of the Vector class is used to return a shallow copy of the vector. It just creates a copy of the vector. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.Example 1: In this example, we use the cl 3 min read Like