• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList- How Safe to use in Jsp  RSS feed

 
mahendra survase
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it a good practice to use Arraylist in jsp compared to vector and all other collection object ?

thanks
Mahendra
SCJP,SCBCD,ICED
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general I would say ArrayList is preferred to Vector.

I would just declare it as a List, as far as a JSP is concerned. That way it doesn't need to bother what the actualy implementation is.

Is it good practice? It depends what you are using the list for, and how you are using it.
 
Eddy Lee Sin Ti
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your data is not shared or read only, ArrayList would be sufficient. If you need to ensure thread safe access to your list, then Vector or other synchronized list (Like those provided in J2SE 5.0) would be more appropriate.

Definitely you can safely use ArrayList for concurrency access, provided that you implements all the necessary synchronization yourself correctly.
 
mahendra survase
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies fellows

Eddy Lee Sin �do you mean not sharing the data among other threads by not creating them in Declaration tag <%! %>.

Thanks
Mahendra
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your data is shared and you want a synchronized list, then just use Collections.synchronizedList(myArrayList), and voila - you have one.
IMO, Vector should be treated as deprecated, and only used when absolutely necessary - ie when imposed on you by someone elses API.


Declaring variables in <%! %> is NOT threadsafe.
However the better approach is to eliminate scriptlet code - don't declare any Lists at all - just use ones from the request/session/application attributes.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!