Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Hashmap

  Asked By: Jasmine    Date: Jul 15    Category: Java    Views: 568
  

Could u plz tell me the difference betweeen the

hashmap and the hash table,

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Sam Anderson     Answered On: Jul 15

I've often wondered this myself. First of all, HashMap is an implementation of
AbstractMap while Hashtable is an implementation of Dictionary. The major
differences are really that you cannot use null values in Hashtable and also
that HashMap is unsynchronized. Being unsynchronized means that a HashMap is
not thread-safe. So, in a Hashtable you can call the methods keys() or
elements() and get an Enumeration() which is a thread-safe way to process all
the keys or elements respectively. HashMap does not give you this ability.

 
Answer #2    Answered By: Mehreen Malik     Answered On: Jul 15

i have taken this content from the source files of the HashMap.java &
Hashtable.java


==========================================================================


Hashmap


Hash table  based implementation of the Map interface. This implementation
provides all of the optional map operations, and permits null values and the
null key. (The HashMap class is roughly equivalent to Hashtable, except that it
is unsynchronized and permits nulls.) This class makes no guarantees as to the
order of the map; in particular, it does not guarantee that the order will
remain constant over time.


This implementation provides constant-time performance for the basic operations
(get and put), assuming the hash function disperses the elements properly among
the buckets. Iteration over collection views requires time proportional to the
"capacity" of the HashMap instance (the number of buckets) plus its size (the
number of key-value mappings). Thus, it's very important not to set the initial
capacity too high (or the load factor too low) if iteration performance is
important.


An instance of HashMap has two parameters that affect its performance: initial
capacity and load factor. The capacity is the number of buckets in the hash
table, and the initial capacity is simply the capacity at the time the hash
table is created. The load factor is a measure of how full the hash table is
allowed to get before its capacity is automatically increased. When the number
of entries in the hash table exceeds the product of the load factor and the
current capacity, the capacity is roughly doubled by calling the rehash method.


Hashtable


This class implements a hashtable, which maps keys to values. Any non-null
object can be used as a key or as a value.


To successfully store and retrieve objects from a hashtable, the objects used as
keys must implement the hashCode method and the equals method.


An instance of Hashtable has two parameters that affect its performance: initial
capacity and load factor. The capacity is the number of buckets in the hash
table, and the initial capacity is simply the capacity at the time the hash
table is created. Note that the hash table is open: in the case of a "hash
collision", a single bucket stores multiple entries, which must be searched
sequentially. The load factor is a measure of how full the hash table is allowed
to get before its capacity is automatically increased. When the number of
entries in the hashtable exceeds the product of the load factor and the current
capacity, the capacity is increased by calling the rehash method.

 
Answer #3    Answered By: Daya Sharma     Answered On: Jul 15

hashmap is unsyncronized and allow a null key and null
value.but hashmap is syncronized and does not allow a
null value.


if u want to make the haspmap object to be syncronized
use.

MashMap obj=Collection.suncronizedMap(new HashMap())

 
Didn't find what you were looking for? Find more on Hashmap Or get search suggestion and latest updates.

Related Topics:



Tagged:  

 
 
 

Related Post