• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList and vector

 
Krishna Tota
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,

I know some thing about arrayList and Vector that is, arrayList is non synchronized and Vector is synchronized.

But i want to know other differences between the two.

And why arrayList is better than Vector in performance.

Please explain in brief.

Buy,
Krishna
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34198
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Krishna Tota:
But i want to know other differences between the two.

They are different implementations of a list. The main differences are the synchronization and that Vector has been around longer.


And why arrayList is better than Vector in performance.

You said this yourself. Each call to Vector executes more logic to check for the synchronization.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, because Vector has been around before the Collections Framework and the List interface were created, it has several methods that you shouldn't use anymore. For instance, use get instead of elementAt, and use add instead of addElement.
 
K Sathya Narayanan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
As Vector methods are synchronized , there is little more overhead need for thread operation like locking the object and releasing the lock. if a element is to added in Vector v , the current thread must get a lock on the vector v and it must release after adding the value. consider you are doing a single threaded where only one thread will access at a time , adding elements to the vector will bring additional thread- overheads which are not necessary, so ArrayList can be used instead of Vector. But in a multi threaded application , where only one thread need to access your data at a given point of time , you can use Vector

Vector was the oldest Collection. The ArrayList was introduced mainly due to the performance issues faced in using the Vector. Hashtable is the oldest Collection. The Hashtable methods are synchronized and it does not allow null values/keys . HashMap is simmilar to Hashtable , but its methods are not synchronized and it allows one null key and multiple null values.

Both Hashtable and HashMap implments the Map interface.

Both ArrayList and vector Implements List interface.

Another important tip

List , Map and Queue interfaces extend the Collection interface
but Map interface does not extends the Collection interface

HTH
K Sathya Narayanan
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can stay away from Vector and use



if your list gets accessed by more than one thread


If you are using JDK 1.5 + then

The �java.util.concurrent� package also has classes like CopyOnWriteArrayList and CopyOnWriteArraySet, which gives you thread safety with the added benefit of immutability to deal with data that changes infrequently. The CopyOnWriteArrayList behaves much like the ArrayList class, except that when the list is modified, instead of modifying the underlying array, a new array is created and the old array is discarded. This means that when a caller gets an iterator (i.e. copyOnWriteArrayListRef.iterator() ), which internally holds a reference to the underlying CopyOnWriteArrayList object�s array, which is immutable and therefore can be used for traversal without requiring either synchronization on the list copyOnWriteArrayListRef or need to clone() the copyOnWriteArrayListRef list before traversal (i.e. there is no risk of concurrent modification) and also offers better performance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic