Vector is a collection class that implements dynamic array data structure to store elements, signifying growable array as its underlying data structure. It accepts duplicate elements and preserves insertion order. It can hold elements of different data types and allows null value. The Vector class was introduced in Java 1.2 version and is part of the original Java API.
It is similar to ArrayList, but is synchronized and supports legacy methods which duplicates the behaviour of methods defined by Collection Framework. It implements Serializable and RandomAccess interface. Its most of the methods are thread safe.
It is a legacy class and fully compatible with Collection, meaning one can use enhanced for loops to iterate through its elements.
1) Its underlying data structure is dynamic array. It has the capability to grow or shrink as required. Like array, it uses zero based index to access, add, delete, and update an element.
2) It is a legacy class, as a result, it supports legacy methods along with predefined methods by Collection framework. Further, it is synchronized.
3) It preserves insertion order that signifies all elements get stored in same order in which they inserted
4) It can be used where multiple threads require to perform operations on same list. However, it can impact performance inversely, since, it’s synchronized.
1. Vector v = new Vector();
The default constructor that creates the list with initial capacity of 10.
When vector crosses its capacity, a new vector object is created by the compiler and the values of first vector object is copied to new one. Lastly, reference variable starts pointing to new one and old reference becomes eligible for garbage collection.
How to calculate the capacity of vector?
capacity of new vector = 2 * current capacity
2. Vector v = new Vector(int capacity);
The constructor creates a vector with the given capacity provided in parameter.
3. Vector v = new Vector(int initialCapacity, int incrementalCapacity);
It creates a vector with initial capacity provided in the parameter. The incrementalCapacity specifies the new incremental capacity every time when the list resized to be allocated .
4.Vector v = new Vector(Collection obj);
It creates equivalent list similar to the collection object provided in the constructor.
Method | Explanation |
---|---|
add(T obj) | It inserts an element to the end and returns true if the element is added successfully |
add(int indx, T obj) | It inserts the given element at the specified index of the list |
addAll(Collection c) | It adds entire compatiable collection objects to the end of the list |
addAll(int indx, Collection c) | It inserts all the compatiable collection objects at the given index in the list |
clear() | It removes or truncates all the elements from the given list |
contains(Object obj) | It returns true if the given object is present within the list |
containsAll(Collection obj) | It returns true if all the elements in the given collection are present in the list |
equal(Object obj) | It returns true if the given object is equivalent to the list |
hashCode() | It returns hashcode of a list |
indexOf(Object c) | It returns the index of the given object which matches first, oterwise -1 is returned |
isEmpty() | It check whether a list contains any element or not. |
iterator() | It returns iterator object for a list |
lastIndexOf(Object obj) | It returns index of the given object which match first from the last, otherwise, -1 is returned |
listIterator() | It returns object of ListIterator |
listIterator(int indx) | It returns listIterator object with the starting point specified with indx |
remove(int index) | It removes an element from the specified position and returns the removed object from the list |
remove(Object obj) | It removes very first occurrence of the given object from the list. |
removeAll(Collection obj) | It removes all the elements that are present in the given collection from the list |
replaceAll(UnaryOperator operator) | It replaces all the elements by performing the given operator to them in the list |
retainAll(Collection c) | It retains all the elements that matches in the given list and removes all others from the list |
set(int indx, T obj) | It replaces the given object at specified position and returns the replaced object. |
size() | It returns number of elements present in the list |
sort(Comparator c) | It sorts the list based on given order specified in the comparator |
spliIterator() | It returns SplitIterator object |
subList(int from, int to) | It returns sublist that falls between fromIndx and toIndex |
toArray() | It converts and returns an array corresponding to the given list |