aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList and vector" Watch "ArrayList and vector" New topic
Author

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.

Buy,
Krishna
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
135

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
K Sathya Narayanan
Greenhorn

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

Joined: May 31, 2007
Posts: 3216
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 Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList and vector
 
Similar Threads
Differentiate ArrayList and Vector
What's diff btw ArrayList and Vector?
ArrayList and Vector
how to synchoronize a ArrayList
Array List vs Vector