-1

I read in the Oracle docs that Vector and Hashtable are Legacy. ArrayList is preferred over Vector. And Hashmap is preferred over Hashtable. I am still relatively new in Java.. Can someone confirm to me that this is indeed the generally accepted practice? Are there places people still use Vector? Thank you

https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html https://docs.oracle.com/javase/tutorial/collections/implementations/index.html

  • Depends if you need need synchronization or not. But even if you do, there are other alternatives which are most likely better than using Vector or HashTable (which is why people can think they are legacy classes). – Pshemo Nov 12 '21 at 16:03
  • 2
    Possibly related: [What are the differences between a HashMap and a Hashtable in Java?](https://stackoverflow.com/q/40471), [What are the differences between ArrayList and Vector?](https://stackoverflow.com/q/2986296) – Pshemo Nov 12 '21 at 16:04

1 Answers1

4

Don't trust us random Internet denizens, trust the javadocs.

The relevant parts outlined :

Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable.

- https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html

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.

- https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html

Of course parallelism (of which thread safety is an aspect) is handled in a whole different way nowadays, so that property on those collections is far less desirable than it once was.

Aaron
  • 24,009
  • 2
  • 33
  • 57