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.

«

»

172 Responses to “Difference between Vector and ArrayList in java?”

  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

  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. Usha.S says:

    It is very easy to UNDERSTAND,Thankyou

  19. Anonymous says:

    vvn vfcb

  20. Anonymous says:

    Thanks very good explanation……….

  21. Anonymous says:

    Thanks.
    But why Vector class is synchronized?
    What is reason behind it?

  22. Anonymous says:

    here is another good link on difference between vector and arraylist

  23. kumaraaswamy says:

    simply super understand

  24. kumaraaswamy says:

    Thanks

  25. Nikitha says:

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

  26. vitthal says:

    my concept is clear
    thanks for that answer
    thanks

  27. 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

  28. krish says:

    nice explanation…

  29. krishnamoorthy.k says:

    sir,
    good explanation and excellent work…

  30. Ramesh says:

    Crispy explanation

  31. ganesh says:

    superbbbbbbbbbbbbbbbbbbb.

  32. 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

  33. surender says:

    also explan about vector size and arraylist pls do needfull

  34. Anuj Pal says:

    impressive work!!

  35. siddu says:

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

  36. Nandkishor says:

    hey you explain collection framework concept nicely….
    be continue

  37. Nandkishor says:

    can any one explain enum in details with example

  38. chaitu says:

    excellent, good , superb

  39. Swethaa says:

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

  40. Latcham says:

    It’s Good Article.Thank You.

  41. niyati says:

    great……..

  42. Anonymous says:

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

  43. fayaz says:

    NICE EXPLANATION SIR

  44. 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.

  45. Amarnath says:

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

  46. Anonymous says:

    thanks joe i think it will help me

  47. vikas pathak(jbp) says:

    great description……..

  48. 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.

  49. easwer says:

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

  50. Anonymous says:

    can u please specify synchronised

  51. ANUMUTHU S says:

    nice Explanation….

  52. Anonymous says:

    love you what a post , chaka maar diya ….

  53. 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..

  54. 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 :)

  55. Suresh says:

    We need Some More Examples about collection..

  56. prakash says:

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

  57. Anonymous says:

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

  58. moumita says:

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

  59. Anonymous says:

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

  60. Dinesh Vhatte says:

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

  61. Anonymous says:

    Very nice explaination

  62. 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 ……

  63. 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);
    }

  64. 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

  65. kashinath says:

    thanks sir very good example

  66. Anonymous says:

    Thanks a lot for giving such knowledge
    Sangeeta sahu

  67. kirubasankar selvaraj says:

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

  68. haritha reddy says:

    its good……..

  69. Anonymous says:

    Very helpful..

  70. 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.

  71. Prem says:

    Its Great!!!!

  72. Santosh says:

    Nice explanation

  73. Merlin says:

    Ya you clarified my doubt more clearly. Thank you.

  74. 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

  75. Pearly says:

    hey joe ..
    could u pls tell me how vector kills the performance..in brief…

  76. rahul says:

    nice explanatio sir,, i like it very informative

  77. 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.

  78. 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.

  79. Anonymous says:

    For concurrent accessing of thread which method in collection is possible ?

  80. Anonymous says:

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

  81. Anonymous says:

    very nice

  82. Anonymous says:

    plz give me examples of arraylist,vector in realtime

  83. Anonymous says:

    Finally i got the answer thank u sir…….

    -Vilas R.Dandge

  84. 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

  85. SRUTI.M.K says:

    THANK U

  86. Anonymous says:

    THANK YOU FOR YOUR WONDERFUL EXPLINATION.

    RAM ON JUNE 25TH,2012 2PM

  87. Anonymous says:

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

  88. santhosh k s says:

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

  89. anubhav says:

    Its easy to understand.

  90. Puneet says:

    Really Helpful.

    Thank You.

  91. Aravindh says:

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

  92. praveen says:

    niceone

  93. 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.

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

  95. 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.

  96. 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.

  97. 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?

  98. moudhani says:

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

  99. minakshi says:

    very nice sir

  100. minakshi says:

    very nice sir

  101. Jeet says:

    Thanks good explanation !!!

  102. JK - Java says:

    Thanks for explanation in simple words and meaning full

  103. nanadha says:

    nice exp….thank u….

  104. nandha says:

    nice exp….thank u….

  105. saddam says:

    hi,
    ur side is very good

  106. Bhushan says:

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

  107. 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 ?

  108. suresh singh says:

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

  109. Muthuraj says:

    Beautifully explained….very nice….Thanks Joe

  110. manoj says:

    Hi joe

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

  111. suman says:

    what is collection object

  112. Anonymous says:

    hey very nice concepts

  113. satya says:

    how to convert 2-dimensional array to hashmap

  114. s says:

    how to convert 2-dimensional array to hashmap

  115. Anonymous says:

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

  116. kishore says:

    how the ArrayList can be synchronized?

  117. kishore says:

    please send the answer to my mailid

  118. Naved says:

    Very good keep it up….

  119. Anonymous says:

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

  120. 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

  121. Michael says:

    Just what I needed.
    Great. Keep it up.

    Thanks

  122. Anonymous says:

    Good Article indeed!

  123. Shirish Herwade says:

    good

  124. mahesh says:

    give some clarification about abstract and encapsulation with some realtime examples

  125. Anonymous says:

    great…

    pratyasha on september 23rd, 2012

  126. vinod says:

    great work sir ,
    very easy to understand for beginners

  127. SardarWaqasAhmed says:

    Thats great explanation..Very easy to grasp the whole theory behind arraylist and vector.

    Thanks

  128. 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…

    Regards:
    Ramesh
    ram009.java@gmail.com

  129. Vikrant Dheer says:

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

  130. 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??

  131. 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);
      }
      }
      }

  132. Anonymous says:

    both will implement random access interface.

  133. 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 !?!?

  134. @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!!!

  135. Anonymous says:

    thank you. very helful

  136. kiran says:

    thank you, very informative

  137. Joseph Kingston Leo . M says:

    very use full tips………..

  138. Nikhil says:

    I really like your theme!

  139. Mark says:

    Great work… more than enough in the difference….

  140. pradip garala says:

    good blog…

  141. Vinto says:

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

  142. Naveen says:

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

  143. Maga says:

    thnxs buddy for your answar………..

  144. u run javapapers mans what says:

    u run javapapers what does it mean fucker

  145. Gopa says:

    Good one. Keep it up.

  146. 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.

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

  148. Mahesh says:

    just started not covered even 0.01 percent of Collections

  149. jlj says:

    Best site for java people

  150. Ravi says:

    Hi Joe,
    Your explanation is awesome. No words to appreciate. Just a small request, please could you update more on collections.

  151. Nanda says:

    Simple with great explanation. simply superb

  152. K Murugan says:

    very good explanation and thanks a lot…..

  153. rahul k k says:

    Excellent explanation….Thanks

  154. 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.

  155. 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 :)

  156. Shivi says:

    Hi Joe,

    Your explanation is great. Wondering if you can provide more info on JCF.

  157. Mayank says:

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

  158. 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….!!!

Leave a Reply