Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector and ArrayList

 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(...));

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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."
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic