Search

Categories

Links

Referers

Prefer ArrayList over Vector

May 11 2005, 02:23:16 AM PDT »Java»Best Practices Comments [5]
Vector is an old class that is fully synchronized. Most often you don't need synchronization on collections and if you do, you can easily synchronize on the collection object.

On a related note, don't use ArrayList for declarations. Always "program to the interface", you should only use the Collection implementations for constructing new collections. For method and variable declarations, use the interfaces. For example, prefer:

List<String> l = new ArrayList<String>();

to

ArrayList<String> l = new ArrayList<String>();

This simple advice will allow you to easily change implementation if, for example, you notice that LinkedList has better performance for you application.

UPDATE: Robert Konigsberg writes: At the same time, prefer Collection to List and Iterable to Collection, although for a different reason.

Which is also good advice. If you only need to iterate over a list or collection, why require List or Collection?

Post a Comment:
Comments are closed for this entry.
Comments:

At the same time, prefer Collection to List and Iterable to Collection, although for a different reason.

Posted by Robert Konigsberg on May 11, 2005 at 11:10 PM PDT #

Collection should be used instead of List when it makes sense. Such as there is an interface and two classes implements it. If one of the implementer is returning a Set , and another is returning a List interface method should return a collection. But collection does not have "all" the facilities of List in it. So, it all depends on your code and how much flexibilty you want ot use.

Posted by aaa on June 30, 2005 at 06:40 AM PDT #

Hi, I'm a survivor of the great JavaForum flame of '04, where this precise topic was argued at a near-infinite length. I should likely not post, but my tickled tongue demands satiation.

There are two really pertinent points that seem to be ignored here: The first is that by using a List in the declaration (as opposed to using it in the definition) one loses all of ArrayList's class specialisation.

List<String> l = new ArrayList<String>(10);

l.add( "one" );
l.add( "two" );
l.trimToSize(); // Throws a compiler error

Of course, the declaration has no place in the class interface so "program from the interface" (write the interface first), isn't really an applicable rule.

The second reason that I dislike the assertion, is that it ignores common use patterns; in the second snippet it should be evident where it is really important to use a List instead of an ArrayList...

interface ListofStrings {
void printList( List<String> strings );
}

If you programme to an interface, here is where the idiom actually is important. It should be pretty obvious that defining printList with an ArrayList parameter would be incorrect, even if you know that you use an ArrayList in the implementation.

Come to think of it, the statement;
List<String> l = new ArrayList<String>();

is a little like writing:

Number number = new Float(22);

it is just too vague, unnecessarily.

Andrew

Posted by Andrew Waddington on August 01, 2005 at 11:28 AM PDT #

One problem with preferring ArrayList is that in many cases Vector is faster. Doing some profiling under JDK 1.4, I found that the performance hit is due to the internal use of iterators in the ArrayList implementation.

Posted by John Lacey on September 15, 2005 at 12:49 AM PDT #

You can see a good example program that clearly shows the difference between them, and when to use ArrayList and when to use Vector. ArrayList or Vector

Posted by Jayaprabhakar Kadarkarai on February 01, 2007 at 04:49 AM PST #

Java is a trademark of Sun Microsystems, Inc.
Copyright © 2006,2007 Peter von der Ahé