The Vector class implements a growable array of objects. It is like the dynamic array which can grow or shrink its size. It is found in java.util package and implements List interface. It is similar to ArrayList but there are two main differences :
It is similar to ArrayList but with two main differences
- Vector is synchronized
- Vector contains many legacy methods that are not part of the collections framework.
Vector is very useful when the array size is not known in advance or array that changes size over liftetime of program
Vector extends AbstractList and implements List<E> interfaace. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. For official documentation on Vector please refere here
Table of Contents
Declaration
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Constructors
There are 3 constructors defined for Vector
- Vector() : Creates a default vector with initial capacity 10
- Vector(int size): Creates a vector whose initial capacity is specified by size.
- Vector(int size, int incr): Creates a vector whose initial capacity is specified by size and increment is specified by incr. It specifies the number of elements to allocate each time that a vector is resized upward.
- Vector(Collection c): Creates a vector that contains the elements of collection c.
If increment is specified, Vector will expand according to it in each allocation cycle but if increment is not specified then vector’s capacity get doubled in each allocation cycle.
Methods of Vector
Here are some of the Vector methods
Adding and retrieving elements from Vector
Vector provides add(Object ob), add(int index, Object obj), addAll(Collection c)
and addAll(int index, Collection c)
to add elements.
import java.util.ArrayList; import java.util.Vector ; public class VectorExample { public static void main(String[] args) { // Create Default Vector Vector vector = new Vector(); vector.add(1); vector.add(2); vector.add("geeks"); System.out.println("Vector after add(Object) : " + vector); vector.add(2, "for"); System.out.println("Vector after add(index, object) : " + vector); ArrayList al = new ArrayList(); al.add("1"); al.add("2"); vector.addAll(al); System.out.println("Vector after addAll(Collection) : " + vector); vector.addAll(3, al); System.out.println("Vector after addAll(index, Collection) : " + vector); vector.insertElementAt("com", vector.size() - 1); System.out.println("Vector after insertElementAt(object, index) : " + vector); vector.get(0); System.out.println("Vector after get(index) : " + vector); } }
Output :
Vector after add(Object) : [1, 2, geeks]
Vector after add(index, object) : [1, 2, for, geeks]
Vector after addAll(Collection) : [1, 2, for, geeks, 1, 2]
Vector after addAll(index, Collection) : [1, 2, for, 1, 2, geeks, 1, 2]Vector after insertElementAt(object, index) : [1, 2, journal, 1, 2, techi, 1, com, 2]
Vector after get(index) : [1, 2, journal, 1, 2, techi, 1, com, 2]
Removinng Elements in Vector
Vector Class provides clear(), remove(index), remove(object), removeAll(Collection c), removeAllElements(), removeElement(Object obj), removeElementAtIndex(index)
and removeIf(Predicate filter)
to remove elements from Vector.
import java.util.Arrays; import java.util.Vector; public class VectorDelete { public static void main(String[] args) { Vector<String> vector = new Vector<String>(Arrays.asList("Sachin", "Dravid", "Ganguly", "Laxman", "Sehwag", "Kohli", "Rohit", "Zaheer", "Kapil", "Gavaskar")); vector.remove(1); System.out.println("Vector after remove(index) : " + vector); vector.remove("Ganguly"); System.out.println("Vector after remove(object) : " + vector); vector.removeAll(Arrays.asList("Laxman", "Sehwag")); System.out.println("Vector after remove(Collection c) : " + vector); vector.removeElement("Kohli"); System.out.println("Vector after removeElement(object) : " + vector); vector.removeElementAt(4); System.out.println("Vector after removeElement(index) : " + vector); vector.removeIf(v -> v.startsWith("Roh")); System.out.println("Vector after removeIf(Predicate p) : " + vector); vector.removeAllElements(); System.out.println("Vector after removeAllElements() : " + vector); } }
Output :
Vector after remove(index) : [Sachin, Ganguly, Laxman, Sehwag, Kohli, Rohit, Zaheer, Kapil, Gavaskar]
Vector after remove(object) : [Sachin, Laxman, Sehwag, Kohli, Rohit, Zaheer, Kapil, Gavaskar]
Vector after remove(Collection c) : [Sachin, Kohli, Rohit, Zaheer, Kapil, Gavaskar]
Vector after removeElement(object) : [Sachin, Rohit, Zaheer, Kapil, Gavaskar]
Vector after removeElement(index) : [Sachin, Rohit, Zaheer, Kapil]
Vector after removeIf(Predicate p) : [Sachin, Zaheer, Kapil]
Vector after removeAllElements() : []
Vector vs ArrayList in Java
Vector and ArrayList both implement List interface and use dynamically resizable arrays for internal data structure.
ArrayList<T> arrList = new ArrayList<T>(); Vector<T> vector = new Vector<T>();
Criteria | Vector | ArrayList |
Synchronization | Vector is synchronized, which means only one thread at a time can access the code | while arrayList is not synchronized, which means multiple threads can work on arrayList at the same time |
Performance | Vector is slow because If one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released. | ArrayList is faster, since it is non-synchronized |
Date Growth | grow and shrink dynamically to maintain optimal use of storage. Vector increments 100% of the current array size if the number of elements exceeds its capacity | grow and shrink dynamically to maintain optimal use of storage. ArrayList increments 50% of the current array size if the number of elements exceeds its capacity. |
Traversal | Vector can use both Enumeration and Iterator for traversing over elements of vector | ArrayList can only use Iterator for traversing |
Conclusion
Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. It is a part of Java Collection framework since Java 1.2. It is recommended to use the Vector class in the thread-safe implementation only.
Thanks, Note
Thanks for spending your valuable time reading this post. Hope this post taught you something new today. Please share the post if you like it.