Java- ArrayList and Vector in java collection or difference between arraylist and vector - JavaTechWorld

Monday, 17 April 2017

Java- ArrayList and Vector in java collection or difference between arraylist and vector

ArrayList , Vector in Java- We should choose list implemented class to store heterogeneous unique or duplicate elements in indexed order. To store elements in index order and to retrieve them randomly.

Specific Functionalities of these two classes- 
  • duplicate objects are allowed in indexed order.
  • heterogeneous objects are allowed.
  • insertion order is preserved.
  • null insertion is possible, more than one null is allowed.
  • initial capacity is 10, incremental capacity is double for Vector and ArrayList use below formula (currentCapacity*3/2+1).
The main difference between these two classes is-

Vector object is thread safe it means synchronized object - so multiple threads cannot modify this object concurrently. it is best suitable in multi-threaded model application to get desired result. But in single thread model application it gives performance issue because for every operation this object should be locked and locked.

ArrayList Object is not thread-safe it means it is not synchronized object it is best suitable in multi-threaded  model application, also in multi-threaded model application if you ensure there is not data corruption.

Array List Constructor-

  • ArrayList()-  This constructor is used to create an empty list with an initial capacity sufficient to hold 10 elements.
  • ArrayList(Collelction<?Extends E> e)- This constructor is used to create a list containing the elements of the specified collection.
  • ArrayList(int initialCapacity)- This Constructor is used to create empty list with initial Capacity.

Method in Array List-

  • boolean add(E e)- This method is used to add elements in add method.
  • boolean add(int index, Element e)- This method is used add element at specified index.
  • boolean addAll(Collection<? extends E>c)- This method is used to add element in specified collection.
  • boolean addAll(int index, Collection<? extends E> c)- This method insert all the element in the specified collection, at a specified position.
  • void clear()- This method will remove all the elements from the list.
  • void clone()- This method returns shallow copy of list element.
  • boolean contains(Object o)- This method return true if element is available int the list.
  • void ensureCapactiy(int minCapacity)- This method increase the capacity of Array List.
  • E get(int index)- This method returns element at specified index.
  • boolean isEmpty()- This method return null if list is empty.
  • int lastIndexOf(Object o)- This method return last occurrence of the specified the list.
  • E remove(int index)- This method remove element at a specified index.
  • int size()- This method returns size of an array list.
  • E set(int index, E element)- This method replace elements at specified position in the list..

Array List Example-

import java.util.*;
Class ListExample {
  public static void main(String... args) {
       ArrayList al=new ArrayList();

   Iterator itr=al.iterator();
   while(itr.hashNext()) {
       System.out.println("Elements are",;

How add mehthod work in ArrayList- So in the above example , we have created an ArrayList object arrobj . To add elements into the arrobj we called the add method on arrobj. After printing the arrobj , we get the desired result ,i.e , values are added to the arrobj.
But the question is how add(Object) method adds the value in ArrayList. So lets find out :

© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.