This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
Joined: Jan 16, 2002
Thank a lot jeff, what does conceptually difference between Vector and Hashtable? Thanks again, Angela
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!
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. Conclusion: 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.