Vector vs ArrayList

 

The three implementations of the List interface are Vector, ArrayList and Linked List.

 

Vector and ArrayList both of these classes are implemented using dynamically resizable

arrays.

 

Vector class is Thread-Safe.

 

Which one you should choose will depend on the requirements of your implementation.

An ArrayList is very efficient for getting objects from the list.

 

The LinkedList is more efficient at insertions into and deletions from the list.

It also performed very well at iterating through the list.

 

The Vector performed worst of all 3 List objects.

The Vector class is less efficient than the ArrayList class mainly because the Vector class

is synchronized

 

 

When multiple threads share the same instance of Vector, then that

instance can be subject to manipulation by both threads without any inconsistency in results.

i.e.,Access to shared data within the Vector is synchronous..One follows the other.

 

Vectors are a lot slower, performance wise, than ArrayLists.

So, my rule is to use Arraylists unless you need synchronised lists.

 

And one more advantage with this Array List is that it belongs to the re-engineered version

of util package, so it has the compatability with the Collections class where you can convert

into various data structures..

 

ArrayList is not synchronized by default –

although the collections framework provides wrappers to provide thread safety when needed.

 

 

For some of the latest JVMs the difference in speed between the two classes is negligible:

strictly speaking, for these JVMs the difference in speed between the two classes is less

than the variation in times obtained from tests comparing the performance of these classes.

 

To visualize the problem with synchronization, try the following code.

 

There is a Producer class that adds 5000 elements to the List (ArrayList/Vector).

Another class, Consumer class removes 5000 elements from the same list.

There are around 10 producer threads and 10 consumer threads.

import java.util.*;

class Producer implements Runnable {

 

private List list;

 

public Producer(List pList) {

list = pList;

}

 

public void run() {

System.out.println(“Producer started”);

for (int i = 0; i < 5000; i++) {

list.add(Integer.toString(i));

}

System.out.println(“Producer completed”);

}

 

}

 

 

import java.util.*;

class Consumer implements Runnable {

private List list;

 

public Consumer(List pList) {

list = pList;

}

 

public void run() {

System.out.println(“Consumer started”);

for (int i = 0; i < 5000; i++) {

while (!list.remove(Integer.toString(i))) {

// Just iterating till an element is removed

}

 

}

System.out.println(“Consumer completed”);

}

}

 

 

import java.util.*;

public class ListTest {

 

public static void main(String[] args) throws InterruptedException {

//List list = new Vector();

List list = new ArrayList();

 

for (int i = 0; i < 10; i++) {

Thread p1 = new Thread(new Producer(list));

p1.start();

}

 

for (int i = 0; i < 10; i++) {

Thread c1 = new Thread(new Consumer(list));

c1.start();

}

Thread.yield();

 

while (Thread.activeCount() > 1) {

Thread.sleep(100);

}

 

System.out.println(list.size());

 

}

 

}

 

 

 

 

Try running the program with ArrayList.

You can see a number of ArrayIndexOutOfBoundException,

Consumer threads will still keep waiting for more elements which wont be added because the

Producer has terminated after throwing the Exception.

 

Now, change the line,

 

List list = new ArrayList();

to

List list = new Vector();

 

Now you can see a proper result.

 

This clearly explains why you should use Vector class when there are multiple threads in the system.

 

In this program, even if you remove the Consumer class and Consumer thread, you can see that the

Producer will themselves throw Exception.

 

This is because, while adding an element to the ArrayList, it checks for the size of the Array.

If the array size is not sufficient, a new array will be created, the elements will be copied to

the new array. If the context switching if Threads happen at this place also, we will get

ArrayIndexOutOfBoundException, or sometimes, you maynot get any Exception, but some elements will

be missing, and many unexpected behaviours.

 

So always use Vector if there are multiple threads. The same rule applies to

HashMap vs Hashtable, StringBuilder vs StringBuffer

 

For Best Java Tutorials Please Visit :

http://sharma.ashutosh84.googlepages.com

Advertisements

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: