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.

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

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);

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.

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.

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.

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.

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 .

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.

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

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.

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

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

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.

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