The moose likes Java in General and the fly likes Vector Vs. Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector Vs. Hashtable" Watch "Vector Vs. Hashtable" New topic

Vector Vs. Hashtable

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
what is difference between Vector and Hashtable?
Please don't send link of Java API because I read in java api, but I am still confused.
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
A Hashtable is a collection of data where each element (piece of data) is accessible via a key. For example, I want to store an Integer(10) and I want to associate that Integer with a key. I would add it to a Hashtable like this:

I may want to add other pieces of data to the hashtable also:

To retrieve my data later (when I need it):

I use the known key to retrieve the data from the collection (Hashtable). Note that if you perform a put using a key that already exists in the Hashtable, the previous data that was associated to the key will be overwritten by the new value.
A Vector is also a collection of data. However, adding and retrieving data is done in a different manner than a Hashtable. Some people think of Vectors as arrays that have no predefined number of elements and that can grow to unlimited size (within the limits of your system). For example, you may assign an array of Integers like this:

myIntegers is an array that may only have up to 10 items within it and can ONLY have objects of type Integer within it. I could do the same thing with a Vector:

Notice that I added an 11th element to the Vector. Notice also that the 11th element was of a different type than Integer.
To retrieve data from a Vector, use the get() method.

Notice the difference between this and Hashtable.
Note also that you may iterate through a Vector and a Hashtable in the same manner:

You should probably do some research on the Collections API. Hashtable and Vector are old data structures that have better implementations in Hashmap and ArrayList.
Hope this has helped!
[ February 11, 2002: Message edited by: Jeff Wisard ]
[ February 11, 2002: Message edited by: Jeff Wisard ]

Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Thank a lot jeff,
what does conceptually difference between Vector and Hashtable?
Thanks again,
Shriram Shivakumar
Ranch Hand

Joined: Jun 20, 2000
Posts: 42
Both Vector and Hashtable can store an array of objects. Think of it like putting an apple, an orange and a pineapple in a conveyer belt. In case of Vector, you need to know at which position each object exists, like apple is at 1, orange is at 2, etc. In case of Hashtable you tag the object with a key. The key can be a number, text,etc., and of course, it has to be unique. When you need an apple, say, then you just refer it using the key.
Hope this explains!

-Shriram Shivakumar
Maciej Kolodziej

Joined: Feb 11, 2002
Posts: 26
There is one important thing about Vector and Hashtable that hasn't been mentioned here.
Hashtable isn't just indexing values with keys. When putting an object into Hashtable (or HashMap) special computation is made on key value, which results in a number. This number is treated, more or less, like an index in Vector. So next time You want to take the object out of Hashtable the same operation is performed on key value You supply and required object is returned from resulting index.
This is very fast, but results in empty, unused space, because computed indexes are not necessarily consecutive, thus it uses more memory.
Use Hashtable (HashMap) when You need to perform frequent search (by key) and Vector (ArrayList) when You just need some not limited space for Your objects.
I highly recommend Sun's Java Tutorial and it's Collections trail.

I agree. Here's the link:
subject: Vector Vs. Hashtable
It's not a secret anymore!