Re: Efficiency - Vectors
Jason Cavett wrote:
Would it be worth it to refactor an application
I'm working on to switch from Vectors to something else (ArrayList)?
Arne Vajh??j wrote:
Choose ArrayList because it is the recommended (the standard) - not
because it is faster.
Choice of collection implementation can be influenced by performance
characteristics in the big-O sense, as well as by other characteristics such
as sortedness.
The API docs for different collections implementations generally comment on
whether they act in constant time, linearly or otherwise with respect to
operations like add(), remove(), indexing and iteration. If you know your app
profile generally favors, say, retrieval over insertion, or that you need
indexed random access rather than sequential, such things might matter.
This leads to the recommended practice of usually declaring the /variable/ to
be of interface type, while implementing the /object/ with a class that has
desirable characteristics, e.g.,
List<String> words = new TreeList<String>();
The algorithm depends only on the abstract behaviors of any List, thus will be
correct with any, but takes advantage of whatever it is TreeList does that you
like. Later, you could revise with LinkedList and not change correctness at all.
The reason to reject Vector is not performance but correctness. It was
pointed out that its synchronization doesn't guarantee thread-safety in all
circumstances, and for single-threaded use its synchronization is unnecessary.
It is the characteristic of synchronization that is not desired here, not
its slowness.
--
Lew