Difference between Vector and ArrayList in java?

java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework.

All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized. All the new implementations of java collection framework is not synchronized.

Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.

Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.

Vector or ArrayList? Which is better to use in java?

In general, executing a ‘synchronized’ method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to be used.

Is there an alternate available in java for Vector?
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.

When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.

When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a costlier operation.

This Java Collection tutorial was added on 04/06/2008.

Comments on "Difference between Vector and ArrayList in java?" Tutorial:

  1. Adish says:

    Good…Keep it up

  2. Sue says:

    Hi Joe,

    Your explanation is great. Wondering if you can provide more info on Java Collections Framework and when to use a particular Collection implementation.

  3. Gaurav Kumar Vij says:

    the diference i read it’s great

  4. Soumik says:

    could u just inform what is the default load factor of ArrayList and Vector

  5. Dipraj says:

    You are not talking about initial capacity when ArrayList or Vector gets created.

  6. Gopi says:

    The default size for Vector & ArrayList is 10

    • Shailendra says:

      This is wrong.
      Default size of arrayList and Vector is zero

      • Singha says:

        @shailendra you just go through the ArrayList class file. There u can that the default size of ArrayList and Vector is 10

        • Manoj says:

          default value of vector and arraylist is 10.

          • muniganesh says:

            Since jdk version 1.7.40, there is no initial capacity for the ArrayList and only the empty ArrayList will be created. You may see the documentation of arraylist with size 10 is created, but when you check its source there is only the emply list is being created and java team did not update that in their document.

  7. kapil says:

    hey thats great. But will u please tell me more about thread safe methods.

  8. upendra singh says:

    hi can you explain me how to iterate through a map
    in java.

    • Rajnish Jha says:

      Hi Upendra,

      Map interface does not extends Iterable interface.So we can not use iterator() method to access the elements of Map classes. However, we can get the collection-view of a map by using entrySet() method .

      Thanks,

      Rajnish.

  9. deepak says:

    excellent

  10. Ramesh says:

    good notes on difference between vector and arraylist……

  11. siva says:

    excellent theory , but its good if it has programmatic explanation also for better understand….

  12. manu says:

    its great answer

  13. sandy rana says:

    this is great answering for diffrent between ArrayList and Vector
    keep it ! up

  14. Gerald says:

    ArrayList has a default size?
    i came to know that only vector has it?
    can you please explain me in detail

  15. suresh says:

    ArrayList has a default Size is 0
    Vector has a default Size is 10

  16. jaya says:

    Vector is synchronized and ArrayList is not synchronized.

  17. Kumaresan says:

    Very nice article

  18. Nikitha says:

    Its excellent . Exactly what I was looking for. Thank you.

  19. Umair says:

    Sir

    Thanks for the great work u are doing. I am just confused on the terms synchronized and non-synchronized , o could you please evaluate these two terms with reference to above article.

    Thanks

    Regards
    Umair

  20. Ramesh says:

    Crispy explanation

  21. surender says:

    also explan about vector size and arraylist pls do needfull

  22. surender says:

    Very good explanation so i have cleared with my doubt i would like to thanks to such a great info could you tell about synchronous and non-sysnchronous methods in vector and arraylist

    warm regards
    surender reddy

  23. Anuj Pal says:

    impressive work!!

  24. siddu says:

    Hi joe,
    this is good but i need more deeply can u provide it ??

  25. Nandkishor says:

    hey you explain collection framework concept nicely….
    be continue

  26. Nandkishor says:

    can any one explain enum in details with example

  27. chaitu says:

    excellent, good , superb

  28. Swethaa says:

    your blog is very usefyl and informative. Your explanation is clear and very easy to understand.

  29. Latcham says:

    It’s Good Article.Thank You.

  30. niyati says:

    great……..

  31. Anonymous says:

    Good way to understood the difference b/w them. Keep it up for good work.

  32. fayaz says:

    NICE EXPLANATION SIR

  33. Abhijit says:

    Nice Explanation but it will add cherry on cake if u explain how vector is Synchronized and how ArrayList is not-Synchronized with An Example.And Multiple Thread can Access vector And ArrayList.

  34. Amarnath says:

    Your explanation is great, I am expecting discuss more in java and post this site.

  35. Anonymous says:

    thanks joe i think it will help me

  36. pramod says:

    Why do we need Vector if we can have synchronized ArrayList? Which one is better Vector or synchronized ArrayList?

    • babji says:

      hi promod,
      if you need storage collection is synchronized u can use vector other wise arralist.you can use synchronize arraylist in the case you want lost with synchronize

      • sasi says:

        Synchronization

        If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
        Only one thread can call methods on a Vector at a time, and there’s a slight overhead in acquiring the lock; if you use an ArrayList, this isn’t the case.

  37. easwer says:

    Hi Sir:
    can you able to give me some tips to select in the interview

  38. Anil Chahal says:

    May u plz tell me how Vector uses thread safe methods while ArrayList uses non thread safe methods..ur rest info is very good..thanks in advance..

  39. Ranj says:

    Hi,

    Superb blog articles – read every one of them !

    One request – can you start one more subsection – Collections? I did find abt read only collections and diff bw Vector/AL, but will appreciate few more, as it is really pretty easy to grasp the topics as you pen them….

    Keep up the good work. One of the very few blogs that I like :)

  40. Suresh says:

    We need Some More Examples about collection..

  41. prakash says:

    its very useful for me…still i need some example….thank u

  42. Anonymous says:

    better to add some code snippets that will understand very easily.

  43. moumita says:

    its very useful for me…though i need some example….
    thank u

  44. Anonymous says:

    Hi its very nice explaination..Still can elaborate using sample coding for synchronised and unsynchronized…..

  45. Dinesh Vhatte says:

    Hi Joe,
    an excellent blog to explain it. arranged it very well.

  46. Anonymous says:

    Very nice explaination

  47. Divya says:

    Hi nice expalnation …
    could please help me ,by letting me know how to do the following ……….

    i have class A with two variable one is string and another is int , know i create an object of this class and add it to a arraylist ….. till here it fine but i am not able to print it ……
    /* something like this
    class A
    {
    String name=”hello”;
    int num=100;
    }
    class TestArrayList
    {
    P S V m(S[] a)
    {
    A objA=new A();
    Arraylist al=new Arraylist();
    al.add(objA);

    // how to print this arraylist ?? :-)

    }
    }
    */
    Thanks in advance ……

  48. Rajesh says:

    A obj = new A();
    ArrayList list = new ArrayList();
    list.add(obj);
    Iterator iterator = list.iterator();
    while(iterator.hasNext()){
    A a = (A)iterator.next();
    System.out.println(a.name);
    System.out.println(a.num);
    }

  49. Shailaja says:

    Hi,

    could u please explain me which one is better to use in JSP pages, whether to use vector or arrayList in JSP Application ??

    Thanks in advance

  50. kashinath says:

    thanks sir very good example

  51. Anonymous says:

    Thanks a lot for giving such knowledge
    Sangeeta sahu

  52. kirubasankar selvaraj says:

    Thanks a lot for sharing precious information about vector ,array and array list.. its really been worth reading.

  53. haritha reddy says:

    its good……..

  54. Anonymous says:

    Very helpful..

  55. senthil kumar m.s. says:

    Thanks for giving an insight about the differences between Vector and ArrayList. As per the JavaDoc for ArrayList says below:

    The details of the growth policy are not
    specified beyond the fact that adding an element has constant amortized time cost.

    So THE GROWTH POLICY OF AN ArrayList CANNOT BE SPECIFIED.

  56. Prem says:

    Its Great!!!!

  57. Anonymous says:

    your explanation is very good is sir and it is easily understandable to everybody but i want to know how can we know that a vector is synchronised. array list is not synchronised

  58. Manas says:

    Hi Joe
    My question is
    “if ArrayList is there,Why we go for vector in collection”.U think due to synchronization.u can do it as ArrayList by SynchronizedList()as thread safe”.

    Which one is used in real time project in IT
    industry.

  59. Arun says:

    i have a point that, vector can grow and can shrink according to the elements present in it. but Arraylist cant, we have to explicitly invoke the method trimtosize() to shrink it.vector is mostly used in web applications.

  60. Anonymous says:

    Please let me know the default size of the arraylist, vector and hash table

  61. Anonymous says:

    plz give me examples of arraylist,vector in realtime

  62. Anonymous says:

    Finally i got the answer thank u sir…….

    -Vilas R.Dandge

  63. Sandhya says:

    Hi Joe,

    Its very good blob, i got a lot of info in this. Would you explain one more difference?

    List list = new ArrayList();
    ArrayList list = new ArrayList();

    what is the difference between above two?

    • Rajnish says:

      Hi Sandhya,

      Both works in the same way however 1st approach is good. In this 1st approach we create a single reference variable of type List which refer to ArrayList object in the constant pool. Suppose we require to create LinkedList object in the same program so we can do like below:
      list = new LinkedList(). So here single reference variable list points to two objects.

      In the 2nd approach we create multiple reference variable pointing respective objects…..not a good approach though..

      Thanks

  64. SRUTI.M.K says:

    THANK U

  65. Anonymous says:

    It would be great if you can put the comparison in a table.

  66. santhosh k s says:

    Hi Joe It’s good, easily can understand the things.
    put it in a Table

  67. anubhav says:

    Its easy to understand.

  68. Puneet says:

    Really Helpful.

    Thank You.

  69. Aravindh says:

    Its very nice definition for beginners like me now i clearly understand the difference thank you.

  70. praveen says:

    niceone

  71. Omprakash says:

    Very nice explanation.. Explanation with an working example would be simply awesome and help in understanding the concepts behind them.

    Thanks for this.

  72. Excellent………
    Good to understand about vector and arraylist

  73. kishore says:

    Now in Java 1.5, instead of using Vector for Synchronized, we can use CopyOnWriteArrayList which is way better than using Vector in certain cases. Please explain the difference between them as its a hot question in interview these days.

  74. Tony says:

    Is there any practical example to prove the statement:-By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.

  75. ilayarajakumaran says:

    Why vector and array list? why applet and swing? why hashmap and hastable? why ejb and spring? why string and string buffer? why errors and exceptions?

  76. moudhani says:

    good explanation….but we need example to understand as practically

  77. Bhushan says:

    Anybody Or Joe
    Please explain internal Array structure how collection are built on top of it

  78. Bhushan says:

    *Initial Capacity of ArrayList and Vector is 10

    *Only difference is that ArrayList doesn’t have the capacity() Method while Vector has.

    Internal implementation is similar for both the classes to set the initial size to 10.

    @Joe
    Challenging question to you ….
    what could be the reason with design prospective , capacity method has not put inside the ArrayList ?

  79. suresh singh says:

    its good way of presenting about vector and arraylist
    plse give with programming
    thanks

  80. Muthuraj says:

    Beautifully explained….very nice….Thanks Joe

  81. manoj says:

    Hi joe

    Tell me what particular situvation arraylist are not used vectors only used

  82. suman says:

    what is collection object

  83. Anonymous says:

    hey very nice concepts

  84. satya says:

    how to convert 2-dimensional array to hashmap

  85. s says:

    how to convert 2-dimensional array to hashmap

  86. Anonymous says:

    We can make arraylist as thread safe then why Vector is still available in Collection?
    Why Java does not deprecate this API?

  87. kishore says:

    how the ArrayList can be synchronized?

  88. kishore says:

    please send the answer to my mailid

  89. Naved says:

    Very good keep it up….

  90. Anonymous says:

    hi,
    your explanation good……..thanks for spending your valuable time…….

  91. Ram says:

    Good explanation.But Can U provide if you have any programmatic experience about vector. Only this is the situation only vector is the solution like that way.

    Thanks

  92. Michael says:

    Just what I needed.
    Great. Keep it up.

    Thanks

  93. Anonymous says:

    Good Article indeed!

  94. Shirish Herwade says:

    good

  95. Ramesh says:

    Hi..

    Good Explanation.

    I have small doubt..

    we can covert the Array list as Synchronization easily..
    and why we go for vector…??

    if it is require to synchronization, we can use Array list Synchronization formula..
    so why vector is required….

    Pl.share me exact ans…

  96. Vikrant Dheer says:

    Hi joe ,
    pls can u share some diffferences between vectors and hash table !!!

  97. pallav rajput says:

    hi joe,

    i am bit confused with Load factor of Arraylist and Vector.

    can u please share some details regarding Load factor??

  98. Anonymous says:

    Hi joe,
    please tell me that how to reverse string in java without using function ?
    e.q
    I am indian.
    output
    indian am I.

    • Rajnish says:

      Here is the code:

      public class ReverseString
      {
      PSVM(String as [])
      {
      String Original=”I am Indian”;
      String Reverse=””;
      int length= Original.length();
      for(int i=length-1; i>=0;i–)
      {
      Reverse=Reverse+charAt(i);
      SOP(Reverse);
      }
      }
      }

  99. Rajakrishna Reddy says:

    Coming to load factor details:
    Vector: Increases by 2 times if capacityIncrement is not specified,

    hence newSize = size * 2;

    else newSize = size + capacityIncrement;

    ArrayList:
    Where as ArrayList directly increments the size by 1.5 times!

    Hence: newSize = ((size * 3)/2) +1;

    But My question is

    AbstractList implements List,
    Vector extends AbstractList and implements List
    Why Vector implements List again!!??

    Anyone knows !?!?

  100. Rajakrishna Reddy says:

    @ram009.java@gmail.com

    Why Vector is used still !!????

    Answer is very simple ….

    Now a days nobody prefers to use vector anymore … but still in JDK for lower versions compatibility only!!!

  101. Anonymous says:

    thank you. very helful

  102. kiran says:

    thank you, very informative

  103. Joseph Kingston Leo . M says:

    very use full tips………..

  104. Nikhil says:

    I really like your theme!

  105. Mark says:

    Great work… more than enough in the difference….

  106. pradip garala says:

    good blog…

  107. Vinto says:

    Its a good work joe…
    If its possible please tell me the way by which i can make an arraylist synchronized….

  108. Naveen says:

    We can use Collections.synchronizedList(List arrayList) method to get the Synchronized list.

  109. Maga says:

    thnxs buddy for your answar………..

  110. u run javapapers mans what says:

    u run javapapers what does it mean fucker

  111. Gopa says:

    Good one. Keep it up.

  112. Muralidhar says:

    Hi Joe, you have covered most of the points wellknown..i need some new points apart from the below points:
    1.Vector is deprecated , ArrayList is not
    2.Vector is Synchronized , ArrayList is not
    3.Vector doubles its size , ArrayList is half when max size reaches
    4.Vector is preferable, because it is threadsafe(allows one by one)
    5. We can Synchronize Arraylist as Collections.synchronizedList(listObj);
    6.Interms of performance Vector is better

    apart from the above points Can you explain on both, how these will work in compiler level.

    Thank you
    Muralidhar N.

  113. […] So while programming, this behaviour should not be banked upon. Example for fail fast iterators are ArrayList, Vector, […]

  114. Bhavesh says:

    How collections are used?

    please reply on my email address
    its urgent.

    reply me on bhavesh71291@gmail.com

    thankyou

    • Rajnish says:

      Hi,

      As per as I know Collections is a class which contains many methods most common methods are sort…to sort the elements within collection and follow quick sort mechanism. Another one is syncronizedList, syncronizedSet and syncronizedMap methods to provide Syncronization version to the collection classes.

  115. Rajnish says:

    Hi Joe,

    I have gone through many books and online tutorials but unable to find the correct answer.. finally I have decided to post you as your explanations on java have been so friendly and nice… So here is my doubts:

    1) How to determine the capacity of an ArrayList? Which method is responsible for determining this in ArrayList? Does the LinkedList class capacity same as that of ArrayList or vector?
    2) Can you please define Fill ratio (Load capacity) in friendly way…not able to get good definition..):
    3) What is the Initial Capacity of HashSet, LinkedHashSet , TreeSet, HashMap, HashTable and TreeMap?

    I know my demand is too much…but I need to know and expecting a quick reply :)

  116. Mayank says:

    ArrayList can perform operation using Iterator only whereas vector can perform operation using Iterator as well as Enumeration.

  117. yogesh sadula says:

    I have been reading all the java posts of yours..Its amazing feeling reading them. You post them with such an ease they become very easy to read and understand. i am looking for more posts and updates from you. If you have any other blogs for other technologies kindly email me. Thanks and great work….!!!

Comments are closed for this "Difference between Vector and ArrayList in java?" tutorial.

↑ Go to top