Java – Hashtable

Prev     Next

  • Hashtable implements the Map interface and extends the Dictionary class.
  • Like HashMap, Hashtable also stores objects as key-value pairs.
  • Hashtable and HashMap are similar except that Hashtable is synchronized and does not allow null values.

Creating a Hash Table:

Syntax:

Hashtable obj = new Hashtable(); //Without Generics

Syntax:

Hashtable<KeyType, ValueType> obj = new Hashtable<KeyType, ValueType>(); //using Generics
  • By default a Hashtable with a capacity of 11 is created and when the size of the Hashtable (number of elements) exceeds 3/4th of the capacity (8), the capacity of the Hashtable is doubled (22).

Example:

Hashtable<Integer,String> Number = new Hashtable<Integer,String>();

Syntax:

Hashtable obj = new Hashtable(int initialCapacity);
  • A Hashtable object is created with the specified initial capacity.
  • When the size of the Hashtable exceeds 3/4th of the initial capacity, the capacity of the Hashtable is doubled.

Example:

Hashtable<Integer,String> Number = new Hashtable<Integer,String>(12);
  • A Hashtable object is created with initial capacity as 12. When 10th element is added, the capacity is doubled to 24.

Syntax:

Hashtable obj = new Hashtable(int initialCapacity, float loadFactor);
  • Creates an empty HashTable object with the specified initial capacity and load factor.
  • Load factor is a value based on which the capacity of the hash table is doubled.
  • When the size of the hash table reaches the value which is the product of initial capacity and the load factor, the capacity of hashmap is doubled

Example:

HashTable<Integer,String> Number = new HashTable<Integer,String>(12,0.5);
  • Hashtable object is created with an initial capacity of 12 and with load factor as 0.5.
  • When the number of elements exceeds the product value (12*0.5 = 6), then the capacity will be doubled.
Methods of Hashtable Description
void clear() Removes all the key-value pairs from the Hashtable.
boolean containsKey(Object key)

 

Returns true, if the Hashtable contains a value for the specified key.
boolean containsValue(Object value) Returns true, if the Hashtable maps a value for the specified key.
Enumeration<V> elements() Returns the values in the Hashtable with the type Enumeration.
Object get(Object key)

 

Returns the value to which the specified key is mapped, or null if the map contains no mapping for the key.
Set entrySet() Returns a Set which contains the key-value pairs of the map.
int hashCode()

 

Returns the hash code value for the Hashtable
boolean isEmpty() Tests if this Hashtable maps no key value pairs.
Enumeration<K> keys() Returns the keys mapped in the Hashtable with the type Enumeration.
Set<K> keySet() Returns the keys mapped in the Hashtable as the type Set.
Value put(K key, V value)

 

Maps the specified key with the specified value in the Hashtable.
void rehash() Increases the size of the Hashtable and re-maps the key values according to the new size.
V replace(K key, V value) Replaces the value of the specified key with the value specified.
int size() Returns the number of key-value pairs in the map.
Value remove(Object key)

 

Removes the key value pair for the specified key from the map if it is present.
Collection values() It returns the collection of values present in the map.

Inserting the key-value pair:

Key value pairs can be mapped by using put() method.

Example:

Output:

{1000=Anjali, 1002=Akila, 1001=Anu}

Removing object from the Hashtable:

To remove the object if the specified key is present in the hashtable.

Syntax:

V remove(Object key)
  • To remove the object from the hashtable if the given key is associated with the specified value.

Syntax:

boolean remove(Object key, Object value)

Example:

Output:

{1000=Anjali, 1002=Akila, 1001=Anu}

The object associated with the key 1002 is deleted

{1000=Anjali, 1001=Anu}

The object with the key 1000 mapped with ‘Anjali’ is deleted

{1001=Anu}

Calculating length of the Hashtable:

Length of the Hashtable can be found using the size() method.

Example:

Output:

{1000=Anjali, 1002=Akila, 1001=Anu}

Size of the Account table:3

Prev     Next





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!