Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vector vs ArrayList

Status
Not open for further replies.

dennismichel

Programmer
Jul 23, 2003
7
BR
Hi
I have a jsp application.
In this program, each user may make simultaneous purchases.
So I need a way to store several itens.
I chose to use a Vector, but I read that's better to use ArrayList, because it's faster than Vector.
In this situation, what should I use?

Thank's dennis
 
The main difference between a Vetor and an ArrayList, is that the Vector is 'thread safe' (meaning that it will not lose data or become unstable when hit from multiple threads) while the ArrayList is not thread safe.
Each call to a jsp should create a new thread so thread saftey should not be a major concern.
An ArrayList may be faster, but you should be sure you want to tamper with a known working jsp before you make the switch.

Good Luck
 
I store the user's Vector in a Session Object.
So, how each user has a session, I think it is possible to use an Arraylist.
When a I need to get the data, I declare a vector variable and set to session object.
This is a thread-safe method?
What do you think?

Thank's dennis
 
I pretty sure that doing this is thread safe. But I'm not certain. You need to ask someone who is an expert on JSP container implementation.
 
>> Each call to a jsp should create a new thread so thread
>> saftey should not be a major concern.

That is not logical. If each request generates a new thread ( that is correct), and a single object can be accessed from those multiple threads (sometimes referred to as a free threaded model) then thread safety IS a concern of course.

>> So I need a way to store several itens.

Use the Vector, until you begin to deal with hundreds of entries the performance difference between the Vector and ArrayList should be minimal.


-pete
 
If the Vector is stored in a Session Object, I think each user has own Vector.
Is it thread-safe?
 
The Vector object is synchronized so it is always thread safe. That's why you should use it in your situation since your application has the potential for using the free thread model.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top