
Java Vector allows the creation of resizable arrays and implements the List interface. The vector object is thread safe, therefore the result will be accurate even if several threads interact with it simultaneously.
The elements of a vector can be retrieved using an integer index. In Java Vector, there are several inherited methods that are no longer part of the collections framework.
Vector is advisable in case of multiple threads because with a single thread, Vector becomes slow.
Table of Contents
Important points to remember about Java Vector
- Vector allows duplicate objects and preserves insertion order, so you don’t need to worry about order.
- In Vector null insertion is possible, so you can insert any number of null.
- Vector implements Serializable, Cloneable interface and RandomAccess interface, because it implements RandomAccess you can access element by index.
- You can insert homogeneous as well as heterogeneous object into vector.
- Vector class is synchronized, hence it is thread safe.
When should you go with Vector?
When your frequent operation is retrieval you should use vector because retrieval is faster in vector.
Note: Default initial capacity of Vector is 10. Once the vector reaches its maximum capacity, a new vector object will be ready with double the capacity. new capacity = current capacity * 2
Constructors of Vector in Java
Vector vector = new Vector();
Vector vector = new Vector(int initialCapactity);
Vector vector = new Vector(int initialCapacity, int capacityIncrement);
Vector vector = new Vector(Collection c);
Generic Vector class in Java
The following are examples of generic vectors:
ArrayList<String> vect = new ArrayList<String>();
ArrayList<Integer> vect = new ArrayList<Integer>(30);
ArrayList<Integer> vect = new ArrayList<Integer>();
vect.add(10);
In the above example, the java compiler internally converts the int type (10) into the Integer type object. In other words, we can say that Vector also accepts elements in the form of object type only.
Program using Java Vector
import java.util.Vector;
public class VectorProgram {
public static void main(String args[]) {
Vector vector = new Vector();
vector.add("Java");
vector.add("C#");
vector.add("PHP");
vector.add("Oracle");
System.out.println("Vector: " + vector);
}
}
You can retrieve vector elements using ListIterator as it iterates the vector in forward and reverse direction.
Difference between ArrayList and Vector
The ArrayList and Vector classes both implement the List interface, both implement the RandomAccess interface and follow insertion order.
There are many similarities between ArrayList and Vector but there are also some differences which are as follows:
# | Vector | ArrayList |
---|---|---|
1 | Vector is thread-safe. | But ArrayList is not thread-safe. |
2 | Every method of Vector is synchronized. | But no any method of ArrayList is synchronized. |
3 | Only one thread is allowed to operate on Vector at a time. | Multiple threads can access ArrayList simultaneously. |
4 | Vector increases a waiting time of threads hence performance is low. | Performance is high because threads are not required to wait. |
5 | Vector is an legacy class so Vector’s many-method is the legacy method. | But ArrayList is not legacy. |
6 | Capacity is increases by new capacity = current capacity * 2 once Vector reaches to maximum capacity. | Capacity is increases by new capacity = current capacity * 3/2 + 1 once ArrayList reaches to maximum capacity. |
7 | Vector can use both Iterator or Enumerator interface to traverse through elements. | ArrayList uses Iterator interface to traverse through elements. |
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/V…r.html