Vector is a legacy class available in the java.util package. The Vector class, a legacy class, was reengineered and included in the Collection Framework.
Vector is the same as ArrayList; it also stores elements using index representation, follows the order of addition, allows random access, and so on.
There are two different between the ArrayList and Vector:
1. ArrayList can be traversed using Iterator and ListIterator, while LinkedList can be traversed using Iterator, ListIterator, and Enumeration.
2. The methods of java.util.ArrayList are not synchronized, while the methods of java.util.Vector are synchronized in nature.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Vector;
public class JTC {
public static void main(String[] args) {
Vector vector = new Vector();
vector.add(101);
vector.add(102);
vector.add(103);
vector.add(104);
vector.add(105);
Iterator iterator = vector.iterator();
System.out.print("Iterator :- ");
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
ListIterator listIterator = vector.listIterator();
System.out.print("\nforward :- ");
while (listIterator.hasNext()) {
System.out.print(listIterator.next() + " ");
}
System.out.print("\nreverse :- ");
while (listIterator.hasPrevious()) {
System.out.print(listIterator.previous() + " ");
}
Enumeration enumeration = vector.elements();
System.out.print("\nEnumeration :- ");
while (enumeration.hasMoreElements()) {
System.out.print(enumeration.nextElement() + " ");
}
}
}
Iterator :- 101 102 103 104 105 forward :- 101 102 103 104 105 reverse :- 105 104 103 102 101 Enumeration :- 101 102 103 104 105
In the above example, as we can see, we have a Vector. In the initial part of the program, we are traversing the Vector using Iterator and ListIterator, similar to ArrayList. In the last section, as we can see, we are creating a java.util.Enumeration type object by invoking the elements() method of the java.util.Vector class, and finally, we are accessing its elements using hasMoreElements() and nextElements() methods.
Note:- Almost all the methods of the java.util.Vector class are the same as those in the java.util.ArrayList class, but they are synchronized.
public class java.util.Vector<E> extends java.util.AbstractList<E> implements java.util.List<E>, java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable {
protected java.lang.Object[] elementData;
protected int elementCount;
protected int capacityIncrement;
public java.util.Vector(int, int);
public java.util.Vector(int);
public java.util.Vector();
public java.util.Vector(java.util.Collection<? extends E>);
public synchronized void copyInto(java.lang.Object[]);
public synchronized void trimToSize();
public synchronized void ensureCapacity(int);
public synchronized void setSize(int);
public synchronized int capacity();
public synchronized int size();
public synchronized boolean isEmpty();
public java.util.Enumeration<E> elements();
public boolean contains(java.lang.Object);
public int indexOf(java.lang.Object);
public synchronized int indexOf(java.lang.Object, int);
public synchronized int lastIndexOf(java.lang.Object);
public synchronized int lastIndexOf(java.lang.Object, int);
public synchronized E elementAt(int);
public synchronized E firstElement();
public synchronized E lastElement();
public synchronized void setElementAt(E, int);
public synchronized void removeElementAt(int);
public synchronized void insertElementAt(E, int);
public synchronized void addElement(E);
public synchronized boolean removeElement(java.lang.Object);
public synchronized void removeAllElements();
public synchronized java.lang.Object clone();
public synchronized java.lang.Object[] toArray();
public synchronized <T> T[] toArray(T[]);
E elementData(int);
public synchronized E get(int);
public synchronized E set(int, E);
public synchronized boolean add(E);
public boolean remove(java.lang.Object);
public void add(int, E);
public synchronized E remove(int);
public void clear();
public synchronized boolean containsAll(java.util.Collection<?>);
public synchronized boolean addAll(java.util.Collection<? extends E>);
public synchronized boolean removeAll(java.util.Collection<?>);
public synchronized boolean retainAll(java.util.Collection<?>);
public synchronized boolean addAll(int, java.util.Collection<? extends E>);
public synchronized boolean equals(java.lang.Object);
public synchronized int hashCode();
public synchronized java.lang.String toString();
public synchronized java.util.List<E> subList(int, int);
protected synchronized void removeRange(int, int);
public synchronized java.util.ListIterator<E> listIterator(int);
public synchronized java.util.ListIterator<E> listIterator();
public synchronized java.util.Iterator<E> iterator();
public synchronized void forEach(java.util.function.Consumer<? super E>);
public synchronized boolean removeIf(java.util.function.Predicate<? super E>);
public synchronized void replaceAll(java.util.function.UnaryOperator<E>);
public synchronized void sort(java.util.Comparator<? super E>);
public java.util.Spliterator<E> spliterator();
}