29

Previously I would always have thought a Vector was good to use for non-descript objects when length was unknown. As far as I was aware I thought it was thread-safe too

What would change that Vector shouldn't be used anymore, and what is the alternative?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
ST.
  • 315
  • 1
  • 4
  • 6
  • 1
    It's curious that `Vector` has not been deprecated. I suppose since there is no one-for-one replacement, they haven't done so. – skaffman Nov 24 '09 at 20:08
  • Note that this is also true of Hashtable--use HashMap instead. – Bill K Apr 01 '13 at 02:47
  • @skaffman `CopyOnWriteArrayList` is more or less a one-for-one replacement insofar as it's a threadsafe `List` implementation. For most usages, `CopyOnWriteArrayList` should offer better performance than `Vector` – Dónal Feb 15 '14 at 00:47

5 Answers5

38

You should use ArrayList instead of Vector. Vector used internal synchronisation, but that is rarely good enough for actual consistency, and only slows down execution when it is not really needed.

Also see this stackoverflow question.

Community
  • 1
  • 1
Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
  • 11
    Good point about rarely being "good enough for actual consistency." If a collection is visible to multiple threads, you should probably be using (and thoroughly understanding) the appropriate collection from `java.util.concurrent`. And if its not accessible to multiple threads, an `ArrayList` is cheaper. Eliminate grey areas where a collection "might be" used by multiple threads: you can't make a collection intrinsically thread-safe; how its clients use it is always a factor. – erickson Nov 24 '09 at 19:06
  • nowadays, do you know if Vector has been improved or if we must stick with ArrayList? – Aquarius Power Nov 12 '14 at 20:07
  • 1
    @AquariusPower The contract specifies that it needs to use synchronization, so it cannot really be improved. That said, a modern JVM will sometimes be able to remove the synchronization as an optimization, but in general, it is just better to use ArrayList. – Paul Wagland Dec 07 '14 at 23:05
16

You can use an ArrayList instead.

If you need a synchronized version, you can do something like:

ArrayList arrayList = new ArrayList();

List synchList = Collections.synchronizedList(arrayList);
hexium
  • 733
  • 1
  • 7
  • 12
8

ArrayList is now the better class to use. Vector is now considered Legacy, and has the added performance overhead of being Thread-Safe.

Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
7

Use ArrayList when you need a List implementation but don't need thread safety, and use CopyOnWriteArrayList when you need a List implementation that is thread safe.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • You need to have in mind that CopyOnWriteArrayList copy the whole array on mutative operations (add, remove ...). CopyOnWriteArrayList should only be used when traversal operations (get, iterate ...) vastly outnumber mutative operations. Source : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html – ihebiheb Jan 16 '19 at 19:20
6

Vector is a legacy collection class from Java 1.0. In Java 1.2 (long ago!), the Collections Framework was added which included new collection classes such as ArrayList and HashMap, which were intended to replace the legacy classes Vector and Hashtable.

As said before, the legacy collection classes had built-in synchronization, which is unnecessary for many applications. Synchronization has a performance overhead, so if it's not necessary, you shouldn't use it.

In some cases (when your program is multi-threaded, and multiple threads access the same data) you need to synchronize your collections. Some people would then use the old Vector or Hashtable classes, but a better way is to use a synchronization wrapper with for example an ArrayList:

// Your standard, unsynchronized list
List<String> data = new ArrayList<String>();

// Use this to put it into a synchronization wrapper
List<String> syncedData = Collections.synchronizedList(data);

See the API documentation of Collections.synchronizedList() (and other methods) for more information.

Jesper
  • 202,709
  • 46
  • 318
  • 350