All too often I see rampant use of the Vector data type in professional Java code. Newly developed code, mind you…not legacy code. I’m not sure exactly what causes these people to use Vector, but when I ask them I usually hear the same reasons. There are actual reasons to use Vector (I will go into this later), but I mostly just hear non-valid reasons. I will now list these reasons out, then crap all over them.
What else would I use?
It blows my mind that professional software developers don’t know of another List data type in Java besides Vector. I would understand this if JDK 1.2 was released recently, but the Java Collections API has been out since December of 1998. That’s over 13 years. That’s literally half of my lifetime (seriously!). It is time to learn the Collections framework. And for those people that say, “Well, Ryan, Vector is in the Collections framework”; Vector was actually retrofitted into the Collections framework for supporting pre-JDK 1.2 applications. That doesn’t mean you should use it all the time. That was the only reason Vector was not deprecated. The Collections framework is probably one of the most useful parts of Java, and it’s a shame that people have not bothered to learn it. Not only have I seen an unnecessary continued use of Vector, I have seen people who do not understand the Collections framework actually change other people’s code (e.g. return types on methods) from List to Vector because they do not understand how Collections work. That’s borderline evil. Even if you need to use Vector, you should always use List as parameters and return types just in case the implementation changes.
Another reason to not use Vector is that it is a synchronized List, which adds unnecessary overhead if you don’t need synchronization. Which brings us to our next excuse…
I need a synchronized List!
I hear this all the time. It’s true, by default, Lists that were added with the Collections framework are not synchronized. There is a very simple solution to this problem:
Collections.synchronizedList is a static factory method that returns a synchronized List that solves this problem. There is no need to use Vector, even if you need a synchronized List.
Reasons to actually use a Vector
There is an instance where you might need to actually use a Vector. I can only think of one instance: you are supporting code that is pre-JDK 1.2. If you are supporting legacy code that was created before the Collections framework was created, then it is possible that you might need to use Vectors.
All that being said, the continued use of Vectors is really nothing more than a minor annoyance in my day to day professional life. Using Vectors is not going to cause the world to end or a software project to fail, and if you were working in an environment where the overhead of using Vectors was that big of a deal, chances are you wouldn’t be using Java in the first place. Maybe it’s just me, but it bugs me when people don’t bother keeping up with languages and technologies that they use everyday for a living. So do your part: if you see someone using a Vector unnecessarily, please try to educate them on the Collections framework. Or at least subtly do so by giving them a copy of Effective Java by Josh Bloch (the author of the Collections framework). Make the world a better place.