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.

Collection Framework

Collection Framework introduced in java 1.2 and its improved a log in the latest versions of Java. Collections are available in java when its started. those are Vector, Stack, HashTable and Array, these are called legacy collection classes.

Collection : Collection is a root interface for the collection framework and it represents a group of objects as single unit. Collection interface consists all the common methods which are used to manipulate the collection(Group of objects).

Collection interface Methods:

  1. public boolean add(Object element) -> Used for insert the elements in a collection.
  2. public boolean addAll(Collection c) -> Used for insert the collection in to a collection.
  3. public boolean remove(Object element) -> Used for remove the specified element from the collection.
  4. public boolean removeAll(Collection c) -> Used for remove the specified collection from a collection.
  5. public boolean retainAll(Collection c) -> Remove the elements from the collection except specified collection.
  6. public int size() -> It return the total number of elements in a collection.
  7. public void clear() -> Removes all the elements from a collection.
  8. public boolean contains(Object element) -> Used for search given element present in a collection or not.
  9. public boolean containsAll(Collection c) -> Used for search a specified collection in a collection.
  10. public Iterator iterator() -> It returns the Iterator object.
  11. public Object[] toArray() -> Converts the collection into a Array.
  12. public boolean isEmpty() -> Used for check the collection is empty or not.
  13. public boolean equals(Object element) -> Used to compare the collection with other collection.
  14. public in hashCode() -> It return hashcode of a collection.