Last Updated: 01 August, 2022
The vector class was introduced in Java 1.0.
Vector is available in the java.util package.
Vector implements the List interface.
Vector is a synchronized and legacy class in Java.
A Vector is similar to an ArrayList in Java. A Vector is a dynamic array of objects that can grow or shrink in size as needed to accommodate adding and removing items after the Vector has been created.
The iterators returned by the Vector class are fail-fast. If the vector is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
As we can see from the complete Vector class hierarchy above, the Vector class extends the AbstractList class and implements List, RandomAccess, Cloneable, and Serializable Interfaces.
Here, E defines the type of elements that the List will contain.
Output
Vector Elements: [India, Canada, US, England]
Java Vector class has the following constructors.
Constructor Name & Descriptions |
---|
Vector() Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero. |
Vector(Collection extends E> c) Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
Vector(int initialCapacity) Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero. |
Vector(int initialCapacity, int capacityIncrement) Constructs an empty vector with the specified initial capacity and capacity increment. |
Java Vector class has the following methods.
Methods Name and Description |
---|
boolean add(Object o) It appends the specified element to the end of this Vector. |
void add(int index, Object element) It inserts the specified element at the specified position in this Vector. |
void addElement(Object obj) Adds the specified object to the end of the vector, increasing its size by one. |
boolean addAll(Collection c) It appends all of the elements in the specified Collection to the end of the Vector. |
boolean addAll(int index, Collection c) It inserts all of the elements in in the specified Collection into the Vector at the specified position. |
Object set(int index, Object element) It replaces the element at the specified position in the vector with the specified element. |
void setElementAt(Object obj, int index) It sets the element at the specified index of the vector to be the specified object. |
Object remove(int index) It removes the element at the specified position in the vector. |
boolean remove(Object o) It removes the first occurrence of the specified element in the vector. |
boolean removeElement(Object obj) It removes the first occurrence of the specified element in the vector. |
void removeElementAt(int index) It removes the element at specified index in the vector. |
void removeRange(int fromIndex, int toIndex) It removes from the Vector all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. |
boolean removeAll(Collection c) It removes from the vector all of its elements that are contained in the specified Collection. |
void removeAllElements() It removes all the elements from the vector. |
boolean retainAll(Collection c) It removes all the elements from the vector except elements those are in the given collection. |
Object elementAt(int index) It returns the element at specified index in the Vector. |
Object get(int index) It returns the element at specified index in the Vector. |
Enumeration elements() It returns the Enumeration of all the elements of the Vector. |
Object firstElement() It returns the first element of the Vector. |
Object lastElement() It returns the last element of the Vector. |
int indexOf(Object element) It returns the index value of the first occurence of the given element in the Vector. |
int indexOf(Object elem, int index) It returns the index value of the first occurence of the given element, search beginning at specified index in the Vector. |
int lastIndexOf(Object elememnt) It returns the index value of the last occurence of the given element, search beginning at specified index in the Vector. |
List subList(int fromIndex, int toIndex) It returns a list containing elements fromIndex to toIndex in the Vector. |
int capacity() It returns the current capacity of the Vector. |
void clear() It removes all the elements from the Vector. |
Object clone() It returns a clone of the Vector. |
boolean contains(Object element) It returns true if element found in the Vector, otherwise returns false. |
boolean containsAll(Collection c) It returns true if all the elements of geven collection found in the Vector, otherwise returns false. |
void ensureCapacity(int minCapacity) It increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument. |
boolean equals(Object o) It compares the specified Object with this vector for equality. |
int hashCode() It returns the hash code of the Vector. |
boolean isEmpty() It returns true if Vector has no elements, otherwise returns false. |
void setSize(int newSize) It sets the size of the vector. |
int size() It returns total number of elements in the vector. |
Object[] toArray() It returns an array containing all the elements of the Vector. |
String toString() It returns a string representation of the Vector. |
void trimToSize() It trims the capacity of the vector to be the vector's current size. |
The Java Vector class's provided methods are implemented in the below-given examples. Let's go through the examples one by one and understand the uses for each method.
Example 1 : Methods for Adding elements in Vector
Output
Vector elements: [100, 200, 300]
After adding 200 at 2nd index: [100, 200, 250, 300]
After adding another vector: [100, 200, 250, 300, 700, 800]
After adding 400 at 4th index : [100, 200, 250, 300, 400, 700, 800]
vectorTwo elements: [1000, 2000, 3000, 4000, 5000]
Example 2 : Methods for Accessing elements from Vector
Output
Vector Elements: [10, 20, 30, 40, 50]
vector.firstElement() : 10
vector.lastElement() : 50
vector.get(3) : 40
vector.elementAt(2) : 30
vector.indexOf(300) : 2
Example 3 : Methods for Updating elements in Vector
Output
Vector elements: [100, 200, 300, 400, 500]
after vector.set(2, 2000) : [100, 200, 2000, 400, 500]
after vector.setElementAt(3000, 3) : [100, 200, 2000, 3000, 500]
Example 4 : Methods for Removing elements from Vector
Output
Vector elements: [100, 200, 300, 400, 500]
After vector.remove(3): [100, 200, 300, 500]
After vector.removeElement(250) : [100, 200, 300, 500]
After vector.removeElementAt(2) : [100, 200, 500]
After vector.removeAllElements() : []
After reinitializing vector : [100, 200, 300, 400, 500]
After clear() : []
Example 5 : Utility methods of Vector Class
Output
vector.isEmpty() : true
Added Vector Elements : [100, 200, 300, 400, 500]
Now, vector.isEmpty() : false
vector.contains(200) : true
vector.contains(8000) : false
Vector Capacity : 10
vector.size() : 5
Array elements : 100 200 300 400 500
Vector subVector elements : [200, 300]
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Vector.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the major difference between a Vector and an ArrayList in Java?
ArrayList and Vector are both classes that implement the List Interface and maintain data insertion order, as well as having a default capacity of 10, but apart from these similarities, both classes have so many major differences. Let's see the table below.
Comparison Parameters | Vector Vs ArrayList |
Synchronization | Vector is a synchronized (thread-safe), so only one thread at a time can access the vector object, whereas An ArrayList is not synchronized (not thread-safe), which means multiple threads can access an ArrayList object at the same time. |
Performance | Vector is by default synchronized so its operations give slower performance, whereas ArrayList is faster since it is non-synchronized. |
Data Growth | ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage. Vector increments 100% , essentially doubling the current array size, whereas ArrayList increments 50% of the current array size if the number of elements exceeds its capacity. |
Traversal | A Vector can use both Enumeration and Iterator for traversing over elements of a vector, whereas an ArrayList can only use Iterator for traversing. |
Legacy Class | Vector is a legacy class. It was introduced in the JDK 1.0 version, whereas ArrayList was introduced in the JDK 1.2 version, and ArrayList is not a legacy class. |