Vector

Vector is a class which extends AbstractList and implements List interface.

  1. Vector is a Legacy collections class. Which is introduced in Java 1.0.
  2. Vector is synchronized, so it can be used in multi threading.
  3. Vector implements Random access interface so we can access the elements from the vector randomly.
  4. Initial capacity of vector is “10” . When it reaches to max then capacity will be increased to double.
  5. All vector specific methods are synchronized.

Constructors:

  1. Vector(): Creates empty Vector with initial capacity “10”.
  2. Vector(int capacity): Creates empty vector with specified capacity.
  3. 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 ;
  4. Vector(Collection c): It creates the Vector which will contain all the elements of the specified Collection.

ArrayList

ArrayList is a Collection class which extends AbstractList and implements List interface.

  1. ArrayList underlying data structure is dynamic grow-able array.
  2. It implements random access interface. So we can access elements from the array list randomly by using index.
  3. 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.
  4. In ArrayList we can’t be used with primitives with generic.(ArrayList a = new ArrayList<>() : It is invalid and compile time error).

Constructors:

  1. 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”).
  2. ArrayList(int initialCapacity): creates the object with specified capacity.
  3. 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 Interface

List is a interface which extends Collection interface.

  1. In List duplicates are allowed and insertion order is preserved.
  2. List is index based so we can access, remove and add elements based on index.
  3. List allows multiple null insertions.
  4. 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.

  1. void add(index i, Object o): It is used for insert the element at the specified index.
  2. boolean addAll(index i, Collection c):Insert the collection in specified index.
  3. Object remove(index i):  Remove the element from the specified index and returns the removed element.
  4. Object get(index i): Get the element from the specified index.
  5. Object set(index i, Object o): Replaces the element in the specified index and returns the old element.
  6. int indexOf(Object o): returns the index of first occurrence of  given element. If element not found then returns -1.
  7. int lastIndexOf(Object 0): returns the index of last occurrence of given element.If element not found then returns -1.
  8. ListIterator listIterator(): it returns the iterator object.
  9. List subList(int minIndex, int maxIndex): Returns the sub list from the main list from mimIndex to maxIndex-1.