20030102 Thursday January 02, 2003

ArrayList vs. Vector - which is better for webapps? I've always thought that it's best to use Vectors in Beans or ActionForms because they are synchronized. However, everytime I write one, I get the feeling that I might as well just use an ArrayList. What do you think? The different between the two is that an ArrayList is unsynchronized (seems strange to me since the it does implement Synchronized - the JavaDocs don't lie do they?). It is easy enough to create a synchronized ArrayList using the following code:

Collections.synchronizedList(new ArrayList());

I like ArrayList better, and I'd like to use it - should I just synchronize it on creation, or use Vectors? Posted in Java at Jan 02 2003, 08:53:58 AM MST 9 Comments

Comments:

Use ArrayList and synchronize (but only as needed). There is a big advantage to using Collections objects everywhere--you can often have functions return Collection and not need to know that your implementation is ArrayList--so that later you can substitute some other kind of Collection as the implementation and not affect the calling code. Also, you probably won't need synchronization in most cases. Any Collection that is request-scoped will be accessed single-threaded (unless you are creating Threads as part of the request). You might only need to synchronize session-scoped collections, of which there should be few, if any.

Posted by Dennis Doubleday on January 02, 2003 at 10:55 AM MST #

I always thought much like the poster above that the ArrayList implementation is the way to go. It seems to be the best Collections implmentation for what you want to do. I was told awhile back Vector sucked and was only still in there for backwards compatablity. I think you should only have to worry about synchronization for shared scope variables (ie application and session variables). --Kurt

Posted by Kurt Wiersma on January 02, 2003 at 11:55 AM MST #

The java.util.ArrayList class doesn't implement "Synchronized", in fact there is no such interface in the standard edition of the Java 2 platform. Synchronization is achieved through enclosing code accessing shared data in <code>synchronized</code> blocks or adding a <code>synchronized</code> identifier to an entire method. When such code is executed it locks the object for which the <code>this</code> reference is valid preventing other threads from modifying the object's state. Synchronization refers to synchronizing several threads' access to such object state. Thanks for your weblog, it's useful, lively and interesting.

Posted by Paul Yunusov on January 02, 2003 at 05:45 PM MST #

You might also check out the FastArrayList in the Jakarta Commons Collections package.

Posted by Brad Smith on January 04, 2003 at 08:58 AM MST #

Posted by 164.164.160.187 on February 10, 2005 at 03:54 AM MST #

i have developed a shopping cart with the help of arraylist.practically it is working properly.

Posted by Rajesh Kumar on December 28, 2005 at 06:29 AM MST #

mkl/

Posted by 220.225.142.21 on December 29, 2005 at 03:51 AM MST #

g

Posted by 203.99.195.2 on May 04, 2006 at 01:29 AM MDT #

zxcC

Posted by 59.144.1.220 on July 03, 2006 at 06:45 AM MDT #

Post a Comment:
  • HTML Syntax: Allowed
Click me to subscribe
Matt Raible is a Web Architect who enjoys developing applications with open source technologies. Contact me for rates.
« December 2008
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   
       
Today

Recent Entries

Tag Cloud