jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes ArrayList and vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList and vector" Watch "ArrayList and vector" New topic

ArrayList and vector

Krishna Tota
Ranch Hand

Joined: Mar 22, 2008
Posts: 40
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.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32614

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Rob Spoor

Joined: Oct 27, 2005
Posts: 20128

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.

How To Ask Questions How To Answer Questions
K Sathya Narayanan

Joined: Feb 15, 2007
Posts: 28
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

K Sathya Narayanan
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3359
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.

[Java-Success.com] | [Java job hunting know how] | [Java resumes]
I agree. Here's the link: http://aspose.com/file-tools
subject: ArrayList and vector
It's not a secret anymore!