Vector is a class which extends AbstractList and implements List interface.
- Vector is a Legacy collections class. Which is introduced in Java 1.0.
- Vector is synchronized, so it can be used in multi threading.
- Vector implements Random access interface so we can access the elements from the vector randomly.
- Initial capacity of vector is “10” . When it reaches to max then capacity will be increased to double.
- All vector specific methods are synchronized.
- Vector(): Creates empty Vector with initial capacity “10”.
- Vector(int capacity): Creates empty vector with specified capacity.
- Vector(int capacity, int incrementalCapacity): Creates empty vector with specified capacity and when it reaches to max capacity then add the incrementalCapacity.newCapacity = oldCapacity + incrementalCapacity ;
- Vector(Collection c): It creates the Vector which will contain all the elements of the specified Collection.
ArrayList is a Collection class which extends AbstractList and implements List interface.
- ArrayList underlying data structure is dynamic grow-able array.
- It implements random access interface. So we can access elements from the array list randomly by using index.
- ArrayList is not synchronized. So if we are using ArrayList in multi threading we need to explicitly make ArrayList as synchronized by using Collection.synchronizedList(List l) method.
- In ArrayList we can’t be used with primitives with generic.(ArrayList a = new ArrayList<>() : It is invalid and compile time error).
- ArrayList(): Creates the object with default initial capacity. (Till Java 1.6 initial capacity is “10” but from java 1.7 initial capacity is “0” but when we add first element then capacity will be “10”).
- ArrayList(int initialCapacity): creates the object with specified capacity.
- ArrayList(Collection c): Creates the object with specified collection data.
When the capacity increased to max then the new capacity will be
int newCapacity = (oldCapacity * 3)/2;
List is a interface which extends Collection interface.
- In List duplicates are allowed and insertion order is preserved.
- List is index based so we can access, remove and add elements based on index.
- List allows multiple null insertions.
- List allows heterogeneous values
List interface is implemented by the ArrayList, LinkedList, Vector and Stack classes.
It has all the functionalities of Collection interface along with below functions.
- void add(index i, Object o): It is used for insert the element at the specified index.
- boolean addAll(index i, Collection c):Insert the collection in specified index.
- Object remove(index i): Remove the element from the specified index and returns the removed element.
- Object get(index i): Get the element from the specified index.
- Object set(index i, Object o): Replaces the element in the specified index and returns the old element.
- int indexOf(Object o): returns the index of first occurrence of given element. If element not found then returns -1.
- int lastIndexOf(Object 0): returns the index of last occurrence of given element.If element not found then returns -1.
- ListIterator listIterator(): it returns the iterator object.
- List subList(int minIndex, int maxIndex): Returns the sub list from the main list from mimIndex to maxIndex-1.