Difference between Vector and ArrayList in java?

04/06/2008

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 posted on 04/06/2008.
Ads by Google

163 comments on “Difference between Vector and ArrayList in java?

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

  2. the diference i read it’s great

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

    • load factor is applicable only to hashed collections

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

  5. The default size for Vector & ArrayList is 10

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

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

        • default value of vector and arraylist is 10.

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

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

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

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

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

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

  11. Vector is synchronized and ArrayList is not synchronized.

  12. Thanks very good explanation……….

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

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

  15. my concept is clear
    thanks for that answer
    thanks

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

  17. sir,
    good explanation and excellent work…

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

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

  20. hey you explain collection framework concept nicely….
    be continue

  21. can any one explain enum in details with example

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

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

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

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

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

    • 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

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

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

  28. love you what a post , chaka maar diya ….

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

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

    • just now i wrote a series of articles on serialization. will soon write a lot on collections.

  31. We need Some More Examples about collection..

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

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

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

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

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

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

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

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

  40. Thanks a lot for giving such knowledge
    Sangeeta sahu

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

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

  43. Ya you clarified my doubt more clearly. Thank you.

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

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

  46. nice explanatio sir,, i like it very informative

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

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

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

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

  51. plz give me examples of arraylist,vector in realtime

  52. Finally i got the answer thank u sir…….

    -Vilas R.Dandge

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

  54. THANK YOU FOR YOUR WONDERFUL EXPLINATION.

    RAM ON JUNE 25TH,2012 2PM

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

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

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

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

    Thanks for this.

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

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

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

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

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

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

  65. Beautifully explained….very nice….Thanks Joe

  66. Hi joe

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

  67. how to convert 2-dimensional array to hashmap

  68. how to convert 2-dimensional array to hashmap

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

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

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

  72. Just what I needed.
    Great. Keep it up.

    Thanks

  73. give some clarification about abstract and encapsulation with some realtime examples

  74. great…

    pratyasha on september 23rd, 2012

  75. great work sir ,
    very easy to understand for beginners

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

    Thanks

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

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

  79. hi joe,

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

    can u please share some details regarding Load factor??

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

  81. both will implement random access interface.

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

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

  84. very use full tips………..

  85. Great work… more than enough in the difference….

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

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

  88. u run javapapers what does it mean fucker

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

  90. Pingback: Fail Fast vs Fail Safe

  91. just started not covered even 0.01 percent of Collections

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

  93. Simple with great explanation. simply superb

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>