Java Hashtable

By: Rahim Vindhani
Hashtable

A collection allows a group of objects to be treated as a single unit. Map is one of the core interfaces of java collection framework that defines operations for maintaining mappings of keys to values.

Map interface does not implement Collection interface, because it does not contain elements but contains entries of keys and their corresponding values (i.e. called mapping).
Map does not allow duplicate keys. So there is utmost one value that is mapped with the given key. Both key and value must be an Object (Primitive values must be wrapped).

Hashtable implements Map interface (As of the Java 2 platform v1.2, this class has been retrofitted to implement Map, so that it becomes a part of Java's collection framework).

Hashtables will automatically grow when you add too many elements. However, growing requires copying, rehashing and rechaining, which affects its overall performance.

Performance of Hashtable depends on two important factors that are 

Initial Capacity and 
Load Factor

Initial Capacity is the capacity at the time the hash table is created. Load factor determines when to increase the capacity of the Hashtable. The default load factor is 0.75.

Important Note: The initial capacity is not the actual number of elements you plan to store in hashtable. Say for example, if you set initial capacity of 100 and the load factor is 0.75, then the capacity of Hashtable will be automatically increased when it reaches to 75 not 100.

Constructors
Hashtable ()
Constructs empty hashtable with a default initial capacity 11 and load factor 0.75.

Hashtable (int initialCapacity)
Constructs empty hashtable with the specified initial capacity and default load factor 0.75.

Hashtable (int initialCapacity, float loadFactor)
Constructs empty hashtable with the specified initial capacity and the specified load factor.

Hashtable (Map t)
Constructs a new hashtable with the mappings same as the passed Map.

Basic methods
Object get (Object key)
Returns the value mapped to the specified key, or null if no entry is found.

Object put (Object key, Object value)
Maps the specified key to the specified value in this hashtable and returns the value previously associated with the specified key, if any. Otherwise, it returns the null value.

Object remove (Object key)
Removes the key and its associated value from the hashtable, and returns the value previously associated with the specified key, if any. Otherwise, it returns the null value.

boolean containsKey(Object key)
Returns true if the specified key is mapped to some value in the map, otherwise false.

boolean containsValue(Object value)
Returns true if there are one or more keys mapped to the specified value, otherwise false.

int size()
Returns the size of the hashtable.

boolean isEmpty()
Returns true if hashtable is empty, otherwise false.

Other methods

void putAll(Map t)
copies all mappings from the map to current hashtable and replaces existing entries, if any.
Void clear()
Removes all mappings from hashtable.

Collection values()
Returns collection of the values contained in the hashtable.

Enumeration elements()
Return Enumeration of the values contained in the hashtable.

Set entrySet()
Returns a Set of entries contained in the Hashtable.

Enumeration keys()
Return Enumeration of keys contained in the hashtable.

Object clone()
Creates copy of the hashtable.

Note : hashtable may throw IllegalArgumentExceptionPsychology Articles, if any unsupported operation is invoked.

Top Searches on
Software
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 

» More on Software