Someone asked me to explain a comment of mine where I mentioned that the Vector class isn't used much any more. Back in the old days, when I was programming and I needed a collection there were no generics in sight yet , I'd use the Vector class as a convenient way of having a list of objects to maintain.
Vector, at the time, could be filled with instances of supertype Object and I got stuck with a lot of casting down to the appropriate type I wanted. In J2SE 1. The following javadoc comments can be found on the Vector class. As of the Java 2 platform v1. We have seen that the Vector synchronizes on each operation and does not synchronize the whole Vector instance itself.
Acquiring the lock once for the whole Vector instance is much more efficient. Vector supports some legacy methods like elements , which returns an enumeration of the components of the vector. Programmers prefer using Iterator or ListIterator preferring Enumeration because:.
As per Java documentation of the Vector class, if a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector. If a thread-safe implementation of List interface is required, we can either use CopyOnWriteArrayList class, which is a thread-safe variant of the ArrayList or synchronize ArrayList externally using the synchronizedList method of the Collections class.
Reference: Vector and Enumeration Javadoc. Vote count: 8. No votes so far! Be the first to rate this post. Primary Primary. Skip to content. Bad Design The major drawback of the Vector class is that it is synchronized but not completely thread-safe. Vector class has a method which return Enumeration over the elements of Vector object. Although, Enumerations are faster than the Iterator, but it is not backed by the original collection.
That means, any changes made to original collection does not reflect in Enumeration object. They ignore the modifications done during iteration. This may cause issues. This makes poor design. This may reduce the performance of your application.
Therefore, instead of using Vector class, always use ArrayList class. You will have re-sizable array and whenever you want to make it synchronized, use Collections.
0コメント