This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Also, if you dont care about data safety when more than one thread accesses the collection (may be bcoz the data is read only), you can use ArrayList to get better performance. Synchronized methods take slightly more time even when there is no contention and time in several orders of magnitude when there is contention to acquire the monitor.
Wait. You should never use Vector. It was left in the language only for classes that require a Vector as input to a method. If you need a synchronized Collection object then use one of the synchronized methods of the Collections class. For example, if you need a synchronized List object use: List myList = new ArrayList(); List mySynchList = Collections.synchronizedList(myList);
Originally posted by Stefan Wagner: If you shouldn't use Vector, why isn't it deprecated? I'm using Vector and have a lot of fun with it: - It works. - It's faster to type than ArrayList. - It's fine, when performance isn't an issue.
It's not deprecated because so many classes that are part of J2SE rely on it and they have no intention of rewriting half of the Java API. The main problem with Vector is that it is not a true member of the Collection family as it has been retrofitted in. It still has a whole series of methods that perform the same function as the Collection methods but are not compatible with other members of the Collection family. There is simply no reason to use Vector ever. When Joshua Bloch rewrote the collection framework he identified Vector and Hashtable as legacy classes that should not be used excpet when required to pass data to an xisting method. I think Joshua Bloch's advice is well worth taking.
Well - that sounds much less dogmatic than 'You should never use Vector'. And you make some unintendet arguments for the Vector-class. There is a lot of code using old-fashion awt methods in applets, and perhaps a lot of code using old Threads. But the problematic code is marked deprecated. If Vector would be a serious problem, it should have been marked deprecated as soon as possible! Marking it deprecated would not mean it will not work as you know. And the more old code uses Vector, the more important it would be to mark it deprecated. If you are a beginner, and don't know the difference between Vector and the alternative collection-classes, you should try to avoid it's usage, to make exchanging of your collection more easy, and not to get used to Vector only. But in an intermediate forum, we can be a little more precise, than giving rules of thumb. P.S.: I almost stoped using Vector in new code, but will not change old code using it, until necessary.