Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList vs. Vector  RSS feed

 
sam leslie
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any real difference between ArrayList and Vector? When should I use one over the other (if it matters at all).
Sam
 
Shakti Sharma
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ArrayList is class from collection framework , recent arrivals on the scene. The package java.util has always contained some other collection. Some are superseded by the new collection types like Vector superseded by ArrayList. Even so, they are not depricated because they are in wide use in existing code and will continue to be used until programmers shift over to new types.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList will never supercede Vector because Vector is synchronized and ArrayList is not. Therefore if you have an situation with multiple threads / users hitting on the same list then you either need to use Vector or wrap ArrayList in something that IS synchronized.
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a synchronized ArrayList you can pass it to the Collections.synchronizedList() function and get back a synchronized version of the list you passed in.
John
 
Mohammed Sardar.
Ranch Hand
Posts: 68
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To keep this up-to-date we can also use copyonwritearraylist is also synchronized...
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not convinced; copy on write is a thr‍ead‑safe class which copies its underlying array whenever there is a change. Its performance whilst adding is slow, so it is not a suitable replacement for a synchronised List. It is intended for thread-safe use whenever the List is read frequently. I think John Wetherbie was correct to recommend the synchronised List method as suggested in the ArrayList documentation.
There is also a copy on write implementation of the Set interface. Read about Collections in the Java™ Tutorials: No 1 and 2.
 
Stephan van Hulst
Saloon Keeper
Posts: 7003
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me Vector became a legacy class when they introduced the Collections Framework in Java 1.2, because they also introduced the Collections.synchronizedList() method.

You should only use Vector when interfacing with old software.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know of one bit of supposedly newer software which still requires a Vector and won't take ArrayList or List. It was updated (I think in Java6) to become Foo<E> rather than Foo, but one bit of its public interface insists on having a Vector<E> as its parameter.
 
Mohammed Sardar.
Ranch Hand
Posts: 68
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should only use Vector when interfacing with old software.
. Is it because that software built with earlier java version ? Could you please illustrate ? thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7003
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see from this discussion, there's already much confusion over whether to use ArrayList or Vector. I would not be surprised if there's still much new code being written using Vector. If you have a dependency on such software, it's best to write your API in such a way that your clients don't have to worry about it. You should use List, Set or just Collection as much as possible in your application, and perform a conversion if a dependency explicitly requires a Vector. How you perform that conversion depends on what operation the dependency performs.

For instance, if they just need a vector of things so they can do some processing on each individual item, and you want to allow any collection of things, then you can do this:

If the dependency needs to edit the actual vector itself, your method should return the result, and document that the input list may be modified:

 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few days ago, I wrote:I know of one bit of supposedly newer software which still requires a Vector and won't take ArrayList or List. . . . .
In case anybody still hasn't identified it yet, you will find it amongst this list of constructors. You can find more such instances via the Vector class-use link.
 
Stephan van Hulst
Saloon Keeper
Posts: 7003
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering Swing has been part of the standard API since Java 1.2, I wouldn't exactly call it newer software.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why I called it supposedly newer software. It was introduced at the same time as the List interface, using only Vector, and that particular class was (at long last) updated from Foo to Foo<E> in Java7 (Java6 version without <E> here), and they didn't update that constructor

It is reasonably simple to find a workaround, however.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!