What is the difference between hash map and hash table?

  
Showing Answers 1 - 21 of 21 Answers

balajipotharaju

  • Sep 29th, 2005
 

among two one is synchronized and another one is not.one to null values also and another willnot take

  Was this answer useful?  Yes

Anitha

  • Nov 2nd, 2005
 

Hashmap is not synchronised, and allows null for key and values where as Hashtable is synchronised and does not allow null values. The elements may not be in an order in the case of hashmap.

Anuragk

  • Apr 1st, 2006
 

By default hash map r not synchronised and allow null values for keys while hashtable is synchronised and not allow null values for keys.

but we can make hash map synchronizale using class named Collections provided with collection framework if required.

  Was this answer useful?  Yes

Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.

The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.

And, a third difference is that HashMap permits null values in it, while Hashtable doesn't. (From jGuru)

Avitra

  • Nov 23rd, 2006
 

Hashmap                                     Hashtable

Not Synchronized                           Synchronized

Null values Accepted                       Null values not accepted

  Was this answer useful?  Yes

Gulzar Ahammed

  • Aug 8th, 2007
 

Hasmap is not syncronized and hash table is  synchronized, Hashmap allows num values in the key value pair where as hashtable doesn't allow null value pairs.

  Was this answer useful?  Yes

Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.

The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.

And, a third difference is that HashMap permits null values in it, while Hashtable doesn't.

For new code, I would tend to always use HashMap.

  Was this answer useful?  Yes

 **hashmap and hashtable are both collection framework data structures
** both datastructres stores data in the form of key,values
**in hashtable key must be String and value must be an object
      hashtable ht=new hashtable();
         ht.put("button",value(new Button("ok"));
                       /                      /
                       key(String)      value(object)
**in hashmap key,value willbe Strings
            hashmap hp=new hashmap();
            String var="india";
 
hp.put("great" ,var);
               /          /
             key       value
            (String)   (String)

  Was this answer useful?  Yes

javabuddy

  • Jan 4th, 2011
 

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

  Was this answer useful?  Yes

Abid

  • Jul 13th, 2011
 

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 does not allow).
HashMap does not guarantee that the order of the map will remain constant over time.
HashMap is synchronized and Hashtable is synchronized.

  Was this answer useful?  Yes

swatinanu

  • Aug 8th, 2011
 

Hash map is basically a collection API which is used to save data in sorted order avoiding duplicates.
where hash table herits Object class.

Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions