X
    Categories: Java Core

Java Hashtable Example

Hashtable implements a hash table (as the name suggests) and maps keys to values (like LinkedHashMap). Hashtable class allows non-null objects to be used as a key or as a value. Just like HashMap, 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 the capacity that was present when the hash table was created. The load factor is a measure of how full the hash table is allowed to get before the capacity is increased by itself.

How does it work?

Hash table uses a hash function whose purpose is to compute an index into an array of slots (or buckets) from which the correct value can be found.

Hashtable workflow

Advantages

  • The average cost of lookup is independent of the number of elements that have been stored in the table.
  • Allows arbitrary insertions and removals of key-value pairs.
  • More efficient in many situations that search trees or any other table.



Constructors summary in Hashtable

  1. Hashtable(): constructs a new hashtable with a default initial capacity (11) (unlike HashMap or LinkedHashMap) and load factor (0.75).
  2. Hashtable(int initialCapacity): constructs a new hashtable with the specified capacity and default load factor (0.75).
  3. Hashtable(int initialCapacity, float loadFactor): constructs a new hashtable with the specified capacity and the specified load factor.
  4. Hashtable (Map<? extends K, ? extends V> t): constructs a new hashtable with the same mappings as the given Map.

Methods in Hashtable Class

  1. void clear: clears the current hashtable which deletes/removes all the key/value pairs.
  2. Object clone(): creates a shallow copy of this hashtable.
  3. V contains(Object value): tests if the current value maps to any keys in the hash table.
  4. boolean containsKey(Object key): tests if the specified key is present in the hashtable.
  5. boolean containsValue(Object value): tests if the specified value is mapped to any keys in the hashtable.
  6. boolean equals(Object o): compares the specified Object with this Map for equality.
  7. V get(Object key): returns the value to which the key is mapped or null if this table/map contains no mapping for the key.
  8. int hashCode(): returns the hash code value for this table/map as per the definition in the Map interface.
  9. boolean isEmpty(): tests if the hashtable maps no keys to values.
  10. V put(K key, V value): maps the specified value to the specified key.
  11. V remove(Object key): removes the specified key from this hashtable.
  12. V replace(K key, V value): replaces the entry for the specified key only if it is currently mapped to some value.
  13. int size(): returns the number of keys in this hashtable.

For more information on the main methods of Hashtable class, feel free to visit the original Oracle documentation.

Removing all keys from a hashtable and cloning a hashtable

import java.util.*; 
class HashTableExample { 
    public static void main(String[] arg) 
    { 
        Hashtable<Integer, String> hashTable = 
                      new Hashtable<Integer, String>(); 
  
        Hashtable<Integer, String> hashTableCopy = 
                      new Hashtable<Integer, String>(); 
  
        hashTable.put(1, "javatutorial"); 
        hashTable.put(2, "dot"); 
        hashTable.put(3, "net"); 
  
        // create a clone of hashtable 'hashTable'
        hashTableCopy= (Hashtable<Integer, String>)hashTable.clone(); 
  
        System.out.println("values in clone: " + hashTableCopy); 
  
        hashTable.clear(); 
  
        System.out.println("after clearing: " + hashTable); 
    } 
}

Output:

values in clone: {1="javatutorial", 2="dot", 3="net"}
after clearing: {}

 

0 0 votes
Article Rating
ym_coding:
Related Post