September 05, 2013

Vector Vs. Hashtable(What is difference between Vector and Hashtable?)

Hashtable: 
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:
    Hashtable myTable = new Hashtable();
    myTable.put("ten", new Integer(10));

I may want to add other pieces of data to the hashtable also:
    myTable.put("nine", new Integer(9));
    myTable.put("today", new java.util.Date());
    myTable.put("user-defined", new UserDefinedClass());

To retrieve my data later (when I need it):
    Integer ten = (Integer)myTable.get("ten");

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.

Vector:
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:
    Integer[] myIntegers = new Integer[10];
    for (int i = 0; i < myIntegers.length; ++i) {
        Integer k = new Integer(i);
        myIntegers[i] = k;
    }

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:

    Vector myVector = new Vector();
    for (int i = 0; i < 10; ++i) {
        Integer k = new Integer(i);
        myVector.add(k);
    }
    // but now I can add more to the Vector
    myVector.add(new java.util.Date());

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.
    Integer three = (Integer)myVector.get(2);


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

    for (Enumeration e = myVector.elements(); e.hasMoreElements(); ) {
        System.out.println(e.nextElement());
    }
    for (Enumeration f = myHashtable.elements(); f.hasMoreElements(); ) {
        System.out.println(f.nextElement());
    }

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.

What does conceptually difference between Vector and Hashtable?
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.

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.

1 comment:

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts