Vector vs ArrayList


Almost all basic java programmers have this doubt in his mind which one is best to use either Vector or ArrayList, Hashtable or HashMap. I don’t want to deviate this topic by evaluating the time required for insertions/deletions/modifications of data in each class instances. Just want to convey a main difference that’s enough for developers to use these classes in a better way. In Java, both the classes are similar from an API perspective but there has to be some differences. Let’s see below.

Vector

  • Vector is thread safe, because vector objects are synchronized which means you can access vector objects from any number of threads at the same time without affecting its data.
  • Due to the above reason, In performance wise Vector is slightly slower than ArrayList
  • Double the size of its internal array when it runs out of space.

ArrayList

  • ArrayList is just opposite of Vector class. Its not thread safe, suppose if you are using ArrayList instances in multiple threads surely which leads to create some problems while accessing.
  • ArrayList comes from Collection Framework family. So its easy to developers to convert in to any other datastructure formats.
  • Increases its array size by 50 percent.

According to me, when you are going to write code without threads it would be better to use ArrayList instead of Vector. Because this will improve your code performance. Anyway based on your requirements you can choose either Vector or ArrayList.

If you want to use/access ArrayList objects concurrently without affecting its data, its possible. In Java, an API is available to achieve this.

List asyncList = Collections.synchronizedList(new ArrayList());

Like Vector vs ArrayList, you can see the differences of Hashtable vs HashMap

Advertisements

Tags: , , , , , , , , , ,

6 Responses to “Vector vs ArrayList”

  1. PingPong Says:

    Hi, the second statement on ArrayList implies that Vector does not belong to the Java Collection Framework. As of Java 1.2 it actually does (cp. http://java.sun.com/javase/6/docs/api/java/util/Vector.html).

  2. manikandanmv Says:

    Not like that. What i meant was ArrayList has already a part of Collection Framework But Vector class has been retrofitted in the Java Collection Framework. (which means Vector class has been modified to implement List interface).

  3. Robbin Says:

    long timer = System.nanoTime();
    List arrayList = new ArrayList();
    arrayList.add(“”);
    arrayList.remove(“”);
    System.out.println(“list: ” + (System.nanoTime() – timer));

    timer = System.nanoTime();
    List vector = new Vector();
    vector.add(“”);
    vector.remove(“”);
    System.out.println(“vector: ” + (System.nanoTime() – timer));

    timer = System.nanoTime();
    List syncList = Collections.synchronizedList(new ArrayList());
    syncList.add(“”);
    syncList.remove(“”);
    System.out.println(“syncList: ” + (System.nanoTime() – timer));

    Execution of the above code will show that vector is the fastest while an unsynchronized list is the slowest!

  4. manikandanmv Says:

    You are misunderstood.!
    Don’t judge which is faster with your simple example. Because this leads to wrong perception.
    You can see the differences while processing huge data. Just try it.
    Hope you are clear now.

  5. Java Tutorial Says:

    One more difference between ArrayList vs Vector is that later is a legacy class and initially not part of Collection API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: