Results 1 to 7 of 7

Thread: HashMap vs HashTable

  1. #1
    Join Date
    Jan 2009
    Posts
    85

    HashMap vs HashTable

    Hello, I am learning java right now. While learning it, I am not able to get the difference between HashMap and HashTable. If I look at the description of both, it looks like similar. So, if anyone knows actual difference then please reply me about the same.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    Re: HashMap vs HashTable

    If you look at the use of the both then you will come to know that they will give you key-value from which you can make use of the data. The Hashtable is the class from the java Collections. And the HashMap is part of Framework of the Collections. If you consider the main difference between then then you will come to know that HashTable is having synchronized access and HashMap is not having synchronized access.

  3. #3
    Join Date
    May 2008
    Posts
    2,015

    Re: HashMap vs HashTable

    HashMap and HashTable is having difference that iterator in the HashMap is fail-safe but if you are using enumerator in your hashtable then it may possible that it will fail while using. If you change the map while iterating then you can able to know this. So, just try it. If you are using HashMap then you can provide the null values in that. But if you are using HashTable then it may not possible that adding null values in that.

  4. #4
    Join Date
    May 2008
    Posts
    2,302

    Re: HashMap vs HashTable

    The HashMap class and Hashtable is quiet similar if you look at the features as well as working of both. But, they are different in there Synchronization strategy. As one will provide it, and other would not. If you make use of the hashmap then it will provide you the facility of the using null values whereas the HashTable won't allow you to use the null value in it. And if you are using the hashmap then it will not provide you the ability of providing constant map, but the HastTable will allow you this facility.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,394

    Re: HashMap vs HashTable

    Take a look at the use of both HashMap and HashTable in coding:
    HashTable:
    Code:
    Hashtable<Integer,String> testing = new Hashtable<Integer,String>();
    testing.put(1, "Lahore");
    testing.put(2, "Karachi");
    testing.put(3, null); 
    System.out.println(testing.get(1));
    System.out.println(testing.get(2));
    System.out.println(testing.get(3));
    HashMap:
    Code:
    HashMap<Integer,String> testing = new HashMap<Integer,String>();
    testing.put(1, "Keys");
    testing.put(2, null);

  6. #6
    Join Date
    Apr 2008
    Posts
    2,010

    Re: HashMap vs HashTable

    HashTable and HashMap both are similar if you look at overall but they are having difference in there behavior.
    Hashtable:
    • It is a Data Structure.
    • It will store your value of Key pairs.
    • It will not provide you use of the null values.
    • It is synchronized.
    • It is having slower performance.

    HashTable:
    • It accepts key value pair.
    • It will provide you the facility of using null value.
    • It is Not Synchronized.
    • It is having faster performance.

  7. #7
    javabuddy Guest

    Re: HashMap vs HashTable

    1. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).
    2. HashMap does not guarantee that the order of the map will remain constant over time.
    3. HashMap is non synchronized whereas Hashtable is synchronized.
    4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't

    to read more about Hashtable vs HashMap see here.

Similar Threads

  1. Hashtable vs Vector
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 01:43 AM
  2. Manipulating a hashtable
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 12:20 AM
  3. Problem sorting a Hashtable
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 04:16 AM
  4. C# Hashtable vs list vs dictionary
    By Zaafir in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 12:36 PM
  5. What is HashMap and Map?
    By TAIPA in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 10:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •