permaculture playing cards*
The moose likes Java in General and the fly likes Vector and ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector and ArrayList" Watch "Vector and ArrayList" New topic
Author

Vector and ArrayList

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
In my architecture framework, I had to use collection depending on my need I had to come to one point either Vector or ArrayList can fit in to my requirement
But the case I am not sure which among the two should be chosen.

As Vector is synchronized and Arraylist is not based on this difference, how do i define where should i chose between this two.
1) Do in my ejb(Session bean and entity bean) would Vector having synchronized nature would be an advantage.
Can I have a scenario in a case where Vector should be used over ArrayList

2) Same for JSP
Can I have a scenario in a case where Vector should be used over ArrayList

Thanks,
aakash
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
If your Collection is never going to be accessed by many threads at a time, then there is no point in using a Vector. You would just be introducing unnecessary synchronization overhead. If you are sure that your Collection will be accessed by only a single thread, go with ArrayList.

However, if you need a synchronized behavior, then you will need to go with a Vector. The usage depends on your need. You can also use an ArrayList and use a mere cynchronized block to protect operations on that ArrayList. Thats another way. But in a multi-threaded scenario use Vector.


Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
No need to go with Vector at any time. The preferred way to go for synchronized Lists is the synchronized version of ArrayList.

From the ArrayList API docs:

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

List list = Collections.synchronizedList(new ArrayList(...));



42
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I'll second Jeroen on this - I can't see any reason why somone developing with a JDK > 1.1 would use Vector at all.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Paul Sturrock:
I can't see any reason why somone developing with a JDK > 1.1 would use Vector at all.
But you save three keystrokes ... and one of them is SHIFTED!!1!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

I can't see any reason why somone developing with a JDK > 1.1 would use Vector at all.


I am speculating at another possible reason being that for it's lack of deprecation. Some of the core APIs depend on Vector.
A better statement might be, "unless you are using dependant APIs, Vector has no place."


Tony Morris
Java Q&A (FAQ, Trivia)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Tony Morris:


I am speculating at another possible reason being that for it's lack of deprecation. Some of the core APIs depend on Vector.
A better statement might be, "unless you are using dependant APIs, Vector has no place."


As Vector now implements List and you should address a class by its interface you should just use a List in those situations (except in the rare cases you depend on Vector-specific behaviour which is unsupported by List).
aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
Thanks,

But still can i have a scenario while using ejb, I had to used synchronized List. As ejb all bean have instances and are not threaded then any scenario in your application you have faced to used synchronized List.

aakash
Ed Lance
Ranch Hand

Joined: Aug 02, 2000
Posts: 38
Above discussion is good but you left out one of the key strengths of the Collection framework. By using ArrayList it's even easier to convert to Sets, HashMaps etc., realize you can still do this with Vector but the method names vary slighty.

It's really about coding to the interface (Collection) not to the implementation, so that WHEN (not if) your requirements change you can take advantage of the good OO Design in the Collections framework to adapt.
 
Don't get me started about those stupid light bulbs.
 
subject: Vector and ArrayList