Re: foreach and stack, iterating from the bottom-up?
Knute Johnson wrote:
Arne VajhHj wrote:
Knute Johnson wrote:
Daniel Pitts wrote:
Third, Use LinkedList or ArrayList instead, Vector is deprecated.
No it's not.
It is not deprecated in the Java sense of the word.
But I would consider it best practice to use ArrayList instead.
The only good argument for not using Vector, since it is a full member
of the Java Collections Framework, is the performance hit from
synchronization. In modern JVMs the performance cost for un-contended
access is very small. And if synchronization is necessary for your
application then Vector is the simpler approach.
I did a simple test to see what the performance looked like on my
computer. I wrote a program to create an ArrayList or Vector, add
200,000 String elements and then remove them, one at a time from the top
of the list. There was no appreciable difference in execution time
between Vector, ArrayList or ArrayList synchronized with the
Collections.synchronizedList() method.
And as we all know, if performance is really the issue, then any list is
a poor choice compared to an array.
I would be willing to bet that the bandwidth used in this never ending
discussion is a bigger performance hit than using Vector.
I don't think the performance is a good reason in most cases.
But you have two classes that implement the same interface with
more or less the same semantics.
It makes sense to use the same class instead of both.
The Vector class is a leftover from Java 1.0/1.1 and I have
no doubt that the intention from SUN is for people to use
ArrayList instead of Vector.
I am also convinced that most people use ArrayList which also
points to using it.
And thirdly the synchronized part of Vector often misleads
developers to write thread unsafe code, because they think
the use of Vector handles it, when it very rarely does.
Arne