ArrayList and Vector difference is very common Java interview question. While beginners tend to use ArrayList for any kind of requirement, the attributes of Vector class should also not be ignored. By having good grasp of both the classes, you can write code which is apt to the requirements.

Similar to ArrayList vs Vector, HashMap vs Hastable is also very frequently asked in interviews. It is good to know the comparison between closely looking classes.

  • While ArrayList doesn’t provide any thread-safety, Vector class has its methods synchronized. This means that calls to member methods on a vector instance are not interleaved where as the calls to member methods of ArrayList class are interleaved. Any method calls on ArrayList need to be synchronized externally by the use synchronized keyword or JDK 5 synchronized collection classes or Apache Commons Collection library.

  • Due to added thread safety by the use of synchronized member methods, the Vector class degrades performance. This performance lag can be seen in multi-threaded applications. When two different threads invoke the same method on the same Vector instance, the calls cannot be interleaved. One thread needs to wait until the other completes its execution of Vector member method.

  • The ArrayList class was introduced in JDK 1.2 where as Vector class has been part of Java since beginning (JDK 1.0).

  • With ArrayList we can only specify the initial capacity. But with Vector, we can not only specify the initial capacity but also capacity increment value.

  • Both the classes allocate more memory to the internal array member variable when initial capacity is overrun. But there are differences how more memory is allocated in two classes. The ArrayList class increases its size by half the initial capacity where as Vector increases its size by the capacity increment value. If the value of capacity increment is 0, the vector increases its size by double the initial capacity in the vector.

  • Developer doesn’t need to handle space time trade off in case of ArrayList. Since Vector allows setting the capacity increment value, setting a very small increment value can result in wastage of processing done in allocating more memory. It is always advisable to set the initial capacity as the optimum elements that the collection class will ever hold.

  • The use of Vector class is not encouraged only for synchronized feature. You should use Vector only when dealing with some legacy applications. ArrayList can be used without any restriction in single threaded classes. If you know the size of elements to be stored and don’t need the collection to grow dynamically, it is better to use array instead of ArrayList.

  • Authors of Vector class are Lee Boynton and Jonathan Payne, while ArrayList has been written by Josh Bloch and Neal Gafter.

  • The serialversionUID for Vector class has been carried over from JDK1.0 as -2767605614048989439L while ArrayList has a serialversionUID of 8683452581122892189L.

  • Both ArrayList and Vector support iterator as both of them extend from AbstractList class and implement List interface. But Vector has support for getting an enumeration over its elements. The iterator returned by both the classes are fail fast where as the enumeration returned by the Vector is not fail-fast.
  • .

Differences between ArrayList and Vector admin Core Java
ArrayList and Vector difference is very common Java interview question. While beginners tend to use ArrayList for any kind of requirement, the attributes of Vector class should also not be ignored. By having good grasp of both the classes, you can write code which is apt to the requirements. Similar...
ArrayList and Vector difference is very common Java interview question. While beginners tend to use ArrayList for any kind of requirement, the attributes of Vector class should also not be ignored. By having good grasp of both the classes, you can write code which is apt to the requirements. Similar to ArrayList vs Vector, <a href="http://www.javaexperience.com/differences-between-hashmap-and-hashtable-in-java/">HashMap vs Hastable</a> is also very frequently asked in interviews. It is good to know the comparison between closely looking classes. <ul> <li>While ArrayList doesn't provide any thread-safety, <strong>Vector class has its methods synchronized</strong>. This means that calls to member methods on a vector instance are not interleaved where as the calls to member methods of ArrayList class are interleaved. Any method calls on ArrayList need to be synchronized externally by the use synchronized keyword or <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/package-summary.html">JDK 5 synchronized collection classes</a> or <a href="http://commons.apache.org/collections/">Apache Commons Collection library</a>. <br/> </li> <li>Due to added thread safety by the use of synchronized member methods, the <strong>Vector class degrades performance</strong>. This performance lag can be seen in multi-threaded applications. When two different threads invoke the same method on the same Vector instance, the calls cannot be interleaved. One thread needs to wait until the other completes its execution of Vector member method. <br/> </li> <li>The ArrayList class was introduced in <strong>JDK 1.2</strong> where as Vector class has been part of Java since beginning (<strong>JDK 1.0</strong>). <br/> </li> <li>With ArrayList we can only specify the initial capacity. But with Vector, we can not only specify the initial capacity but also capacity increment value. <br/> </li> <li>Both the classes allocate more memory to the internal array member variable when initial capacity is overrun. But there are differences how more memory is allocated in two classes. The ArrayList class increases its size by half the initial capacity where as <strong>Vector increases its size by the capacity increment</strong> value. If the value of capacity increment is 0, the vector increases its size by double the initial capacity in the vector. <br/> </li> <li>Developer doesn't need to handle <strong>space time trade off</strong> in case of ArrayList. Since Vector allows setting the capacity increment value, setting a very small increment value can result in wastage of processing done in allocating more memory. It is always advisable to set the initial capacity as the optimum elements that the collection class will ever hold. <br/> </li> <li>The use of Vector class is not encouraged only for synchronized feature. You should use Vector only when dealing with some <strong>legacy applications</strong>. ArrayList can be used without any restriction in single threaded classes. If you know the size of elements to be stored and don't need the collection to grow dynamically, it is better to use array instead of ArrayList. <br/> </li> <li>Authors of Vector class are <strong>Lee Boynton and Jonathan Payne</strong>, while ArrayList has been written by <a href="http://en.wikipedia.org/wiki/Joshua_Bloch">Josh Bloch</a> and <strong>Neal Gafter</strong>. <br/> </li> <li><a href="http://www.javaexperience.com/java-role-of-serialversionuid-in-serialization/">The serialversionUID</a> for Vector class has been carried over from JDK1.0 as -2767605614048989439L while ArrayList has a serialversionUID of 8683452581122892189L. <br/> </li> <li>Both ArrayList and Vector <a href="http://www.javaexperience.com/java-how-iterators-works/">support iterator</a> as both of them extend from AbstractList class and implement List interface. But Vector has support for getting an enumeration over its elements. <strong>The iterator returned by both the classes are fail fast where as the enumeration returned by the Vector is not fail-fast.</strong></li> .</ul>
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.