0

I know this question can be a little stupid but I just want to clear the doubt. When going through Java tutorial for Collection ( http://docs.oracle.com/javase/tutorial/collections/index.html ), I didn't find any relevant information about both Vector and Hashtable. Both belong to Collection framework as Vector is implementation of List and Hashtable is implementation of Map. If it is so then why it is not in Sun tutorial? Where can I find Sun tutorial for Collection which contain good doc about both Vector and Hashtable and in depth knowledge about elements storing in List, Set and Map?

Nizam
  • 573
  • 1
  • 5
  • 12

4 Answers4

6

Because Vector and Hashtable are old, legacy collection classes. Don't use them.

Instead of Vector use ArrayList; instead of Hashtable use HashMap.

When Java 1.2 was released (very long ago), new collection classes were added to Java (the Collections Framework). Sun did not remove the old classes such as Vector and Hashtable because they wanted the new Java version to be backwards compatible. And now we still have those old classes.

One difference to be aware of is that Vector and Hashtable are synchronized, while ArrayList and HashMap are not. Most of the time you don't need synchronization; if you do, then you must take care to synchronize your ArrayList, and if you need a map, use ConcurrentHashMap instead of plain HashMap.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Thanks Jesper for the answer. But can I get old Java tutorial for Collection that include Vector and Hashtable like the recent one on Collection by Sun. Secondly any tutorial which say about in depth knowledge about elements storing in List, Set and Map (Sun doc will be preferred)? – Nizam May 13 '14 at 09:54
  • 1
    You can lookup those classes in the [API documentation](http://docs.oracle.com/javase/8/docs/api/). They work almost the same as other list and map classes, so why do you need a special tutorial for these old classes? The tutorial you already found explains exactly how to use lists, sets and maps. – Jesper May 13 '14 at 09:56
  • I read about collection and it say hashset stores its elements in a hash table and hashtable stores in bucket. I am just eager to know what is bucket. how element is added in that and when remove it is remove from rear end or front end. How hashcode helps a hashtable. Does hashcode is also stored in bucket. if so how etc etc. I hope you understand what I want to tell. – Nizam May 13 '14 at 10:16
  • Those are questions about how `HashMap` or `Hashtable` works internally - not things you would find in a tutorial on how to use these classes. See [my answer on this question](http://stackoverflow.com/a/6493946/135589) for an idea of how it works. – Jesper May 13 '14 at 13:51
1

In general, Vector and Hashtable could be considered deprecated.

If you look at the online javadoc for Vector and Hashtable you'll see that they were the original implementation of ArrayList and HashMap, until the Collections framework came along, at which point they were retrofitted to implement interfaces from the Collections framework; this way, old classes that depended on those classes being there would not break. The only difference between them and their more common brethren is that they are synchronized.

In the vast majority of cases, synchronization isn't called for, so programmers will avoid the synchronization overhead and opt for regular ArrayLists and HashMaps. If a synchronized collection is desired there's always Collections.synchronized____() (or ConcurrentHashMap) that would work just fine too.

You probably don't need a tutorial for Vector and Hashtable because their behavior is already so similar to classes you're likely to be familiar with, and because they aren't used much any more. As for more info on List, Set, and Map, the online javadoc is a good place to start.

awksp
  • 11,764
  • 4
  • 37
  • 44
0

As mentioned by the JavaDoc of Vector:

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.

it is kind of a legacy implementation of the List interface. The whole collection framework has been implemented to be by default not thread-safe. If you need thread safety, you may wrap any non tread-safe implementation by using the proper Collections.synchronizedXXX() methods, where XXX is List or Map or Set for example. The same applies for HashTable, which is by default synchronized as well. You should use HashMap instead and Collections.synchonizedMap() instead.

Harmlezz
  • 7,972
  • 27
  • 35