• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ArrayList and vector

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41998
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic