Vector in Java
Vector in Java
A vector in Java is like a dynamic array whose size can increase or decrease. In Java,
vectors do not have any size limit like arrays and they can store any number of
elements. It was introduced in Java 1.2, and has since become part of the Java
collection framework.
The Vector class is in the java.util package and implements the List interface. Therefore,
you can use all the methods of the List interface here.
Legacy Classes:
Some classes i.e. Vector, Stack, Hashtable etc. was introduced in JDK 1.0 version
these classes where modified or say re-engineered so that they can be adjusted in new
collection hierarchy, so these older classes are known as legacy classes.
A vector in Java is a class provided by the java.util package that represents a dynamic
array-like data structure. It is part of the old java collection framework and is similar to
ArrayList with a few differences. A vector can dynamically increase or decrease in size
when elements are added or removed.
Suppose we have to create a tree data structure and store the value of the node.
Because we do not already know the number of nodes, we need a suitable data
structure that meets our needs.
We can use Array, but it is initialized with a fixed size, so it cannot be changed in the
future. Also, if the array becomes full, it can cause issues, especially if we want to insert
more nodes into the tree.
Since we cannot extend Arrays, we need another dynamic data structure whose size
can be increased or decreased depending on our needs. This is where vectors come in
handy in Java.
Syntax
SN Method Description
8) containsAll() It returns true if the vector contains all of the elements in the
specified collection.
9) copyInto() It is used to copy the components of the vector into the specified
array.
12) ensureCapacity() It is used to increase the capacity of the vector which is in use, if
necessary. It ensures that the vector can hold at least the number of
components specified by the minimum capacity argument.
13) equals() It is used to compare the specified object with the vector for
equality.
15) forEach() It is used to perform the given action for each element of the
Iterable until all elements have been processed or the action throws
an exception.
16) get() It is used to get an element at the specified position in the vector.
18) indexOf() It is used to get the index of the first occurrence of the specified
element in the vector. It returns -1 if the vector does not contain
the element.
19) insertElementAt() It is used to insert the specified object as a component in the given
vector at the specified index.
23) lastIndexOf() It is used to get the index of the last occurrence of the specified
element in the vector. It returns -1 if the vector does not contain
the element.
24) listIterator() It is used to get a list iterator over the elements in the list in proper
sequence.
25) remove() It is used to remove the specified element from the vector. If the
vector does not contain the element, it is unchanged.
26) removeAll() It is used to delete all the elements from the vector that are present
in the specified collection.
27) removeAllElements() It is used to remove all elements from the vector and set the size of
the vector to zero.
30) removeIf() It is used to remove all of the elements of the collection that satisfy
the given predicate.
31) removeRange() It is used to delete all of the elements from the vector whose index
is between fromIndex, inclusive and toIndex, exclusive.
32) replaceAll() It is used to replace each element of the list with the result of
applying the operator to that element.
33) retainAll() It is used to retain only that element in the vector which is
contained in the specified collection.
34) set() It is used to replace the element at the specified position in the
vector with the specified element.
35) setElementAt() It is used to set the component at the specified index of the vector
to the specified object.
37) size() It is used to get the number of components in the given vector.
38) sort() It is used to sort the list according to the order induced by the
specified Comparator.
39) spliterator() It is used to create a late-binding and fail-fast Spliterator over the
elements in the list.
40) subList() It is used to get a view of the portion of the list between fromIndex,
inclusive, and toIndex, exclusive.
41) toArray() It is used to get an array containing all of the elements in this
vector in correct order.
43) trimToSize() It is used to trim the capacity of the vector to the vector's current
size.
Difference between ArrayList and Vector
ArrayList Vector
ArrayList was introduced in JDK 1.2. Vector class was introduced in JDK 1.0
ArrayList is not a legacy class. Vector is a legacy class.
ArrayList is not synchronized. Vector is synchronized.
ArrayList is not thread-safe Vector is thread-safe
ArrayList is fast because it is non- Vector is slow because it is synchronized,
synchronized. i.e., in a multithreading environment, it
holds the other threads in runnable or non-
runnable state until current thread releases
the lock of the object.
ArrayList does not generable for data Vector provides generable for data
consistency consistency
In case of ArrayList new capacity In case of Vector new capacity
=[(old Capacity*3)/2]+1; =[old Capacity*2];
=[(10*3)/2]+1=16 =[10*2]=20
ArrayList does not provide any method to Vector class provide a method that is “int
find its capacity capacity” to find the capacity of vector.
// Program 1
import java.util.*;
//Create a vector
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
Output:
// Program 2
import java.util.*;
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
if(vec.contains("Tiger"))
else
}
Output:
Size is: 4