This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Unless you dont have any specific requirment of synchronisation , you shuld use ArrayList.It will improve the speed because vector takes time in synchronisation of its elements which is not the case with ArrayList.
"There are no mistakes, only lessons"
Joined: Jul 05, 2005
can u tell me under which conditions we have to use Vector not array list with some example code
This question that you have asked is more generic. There are n reasons for when to go for Vector and when for array list. But, it all depends on the application.
Servlet runs on server side. It is instantiated once (but depending on webserver, there can be more than 1 instance for load balancing) and whenever a user request for its service, ONE NEW THREAD for this servlet is created for you.
Thus the entire service method should be synchronized. I mean it should handle concurrent access of the member variables that your servlet possess. So, if you have a ArrayList in your servlet, you will have to take care of its synchronization.
When multiple threads of the same servlet access this ArrayList, it should lead to consistent data read/store and prevent dirty reads.
Hope you got me. Vector might be good I believe.
All those experts there ... correct me If I am wrong!
Originally posted by vidhyasagar reddy: can u tell me under which conditions we have to use Vector not array list with some example code
If u r adding instance variables to arraylist there may be some sharing violations i.e when two users are accessing same object.So, try to use synchronized blocks while accessing instance variables stored in arraylist.