Vector vs ArrayList

By manikandanmv

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

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.

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, the same differences are applies to Hashtable vs HashMap respectively

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

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

Leave a Reply