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.
The Java class Vector has been a part of the JDK since version 1.0.
In J2SE 1.2 (JDK 1.2) the Collections framework was introduced.
The following javadoc comments can be found on the Vector class.
As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.[1]
Also the following text is displayed in Netbeans if you do try to use the Vector class.[4]
"This inspection reports any uses of java.util.Vector or java.util.Hashtable. While still supported, these classes were made obsolete by the JDK1.2 collection classes, and should probably not be used in new development."
Basically, what I'm saying is, that Vector class was implemented before the Collections framework. As such it does not follow the Collections framework naming scheme of List,Set and Map. It also has a number of methods, for backwards compatibility, that are not to be found in any of the other collection classes.
If you do need synchronization, the Vector is indeed synchronized, yet it is synchronized on each and every operation. This is fine, if you do not need to execute a lot of operations as an atomic transaction. But, for instance, if you need to iterate over all the items in the Vector, it is always better to get a lock before the iteration and release it after the iteration.
So, in short:
- use ArrayList, if you do not care for the synchronization aspect of Vector
- use Collections.synchronizedList[2] if you need an array that is synchronized
- use CopyOnWriteArrayList if you are dealing with multiple concurrent read operations and only a few write operations *)
- use a native array, if you do not care for synchronization and performance is an issue.
In the javadoc of synchronizedList there is a good example of iterating over it. I shall just write it down here for completeness.
List list = Collections.synchronizedList(new ArrayList());
...
synchronized (list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
As always in these things, the requirements you have, determine what kind of Collection you need to use....
synchronized (list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
Updates
23/10/2012: Updated because of the comment below on CopyOnWriteArrayList.
28/01/2013: Update: the same goes for HashTable (old) and HashMap (new).
References
- [1] Vector
- http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
- [2] public static <T> List<T> synchronizedList(List<T> list)
- http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29
- [3] Why is Java Vector class considered obsolete or deprecated?
- http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated
- [4] Is Vector an obsolete collection class?
- http://www.coderanch.com/t/515352/java-developer-SCJD/certification/Vector-obsolete-collection-class
- [5] Vector or ArrayList -- which is better?
- http://www.javaworld.com/javaqa/2001-06/03-qa-0622-vector.html
Don't forget the CopyOnWriteArrayList if you are dealing with multiple concurrent read operations and only a few write operations.
ReplyDeleteThank you. I didn't mean this post to be exhaustive on the possible alternatives, but I've added it to the options none the less.
Delete