This is one of the famous questions that a Java beginner has in his mind. This is also a famous question asked in interviews. Following are the differences between ArrayList and Vector.
1. Vectors and Hashtable classes are available from the initial JDK 1.0. But, ArrayList and HashMap are added as a part of new Collections API since JDK 1.2.
2. Vectors and Hashtable are synchronized where as ArrayList and HashMap are unsynchronized.
When to use Vector? When to use ArrayList?
1. ArrayList is faster when compared to Vector since ArrayList is unsynchronized. So, if the List will be modified by only one thread, use ArrayList. If the list is a local variable, you can always use ArrayList.
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.
Now, change the line,
to
and run the program.
Now you can see a proper result.
Summary:
1. Use Vector if there are multiple threads and ArrayList if there is only a single thread.
2. Use Hashtable if there are multiple threads and HashMap if there is only a single thread.
3. Use StringBuffer if there are multiple threads and StringBuilder if there is only a single thread.
1. Vectors and Hashtable classes are available from the initial JDK 1.0. But, ArrayList and HashMap are added as a part of new Collections API since JDK 1.2.
2. Vectors and Hashtable are synchronized where as ArrayList and HashMap are unsynchronized.
When to use Vector? When to use ArrayList?
1. ArrayList is faster when compared to Vector since ArrayList is unsynchronized. So, if the List will be modified by only one thread, use ArrayList. If the list is a local variable, you can always use ArrayList.
2. If the List will be accessed by multiple threads, always use Vector, otherwise you should take care of synchronization manually.
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.
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");
}
}
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");
}
}
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();and run the program.
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 may not get any Exception, but some elements will be missing, and many unexpected behaviors.
So always use Vector if there are multiple threads. The same rule applies to HashMap vs Hashtable, StringBuilder vs StringBuffer.
Summary:
1. Use Vector if there are multiple threads and ArrayList if there is only a single thread.
2. Use Hashtable if there are multiple threads and HashMap if there is only a single thread.
3. Use StringBuffer if there are multiple threads and StringBuilder if there is only a single thread.
No comments:
Post a Comment