Wednesday, July 3, 2024
HomeLanguagesJavaJava Program to Implement HashTable API

Java Program to Implement HashTable API

The Hashtable class implements a hash table, 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.  

To implement Hashtable API first, we create a class “Hashtable” and create all the methods of the Hashtable in this class:

Java




// Java program to implement the hashTable API
  
import java.util.*;
import java.util.Map.Entry;
   
class HashTableImplementation<K, V>
{
    private Hashtable<K, V> hashTable;
   
    // Constructor creates a new HashTable 
    public HashTableImplementation()
    {
        hashTable = new Hashtable<K, V>();
    }
   
    // Constructor creates a new empty Hashtable 
    // according to the given Hashtable
    public HashTableImplementation(Map<? extends K, ? extends V> hashTable1)
    {
        hashTable = new Hashtable<K, V>(hashTable1);
    }
   
    // Removes all of the elements from the hashTable
    public void clear()
    {
        hashTable.clear();
    }
   
    // Creates a shallow copy of the hashtable
    public Object clone()
    {
        return hashTable.clone();
    }
   
    // Check whether the given Object contains in the hashTable
    public boolean contains(Object obj)
    {
        return hashTable.contains(obj);
    }
   
    // Returns true if the hashtable contains given value otherwise return false
    public boolean containsValue(Object val)
    {
        return hashTable.containsValue(val);
    }
   
    // Returns true if the hashTable contains given key otherwise return false
    public boolean containsKey(Object key)
    {
        return hashTable.containsKey(key);
    }
   
    // Returns an enumeration of the values in the hashtable
    public Enumeration<V> elements()
    {
        return hashTable.elements();
    }
   
    // Returns a set of entry of hashTable
    public Set<Map.Entry<K, V>> entrySet()
    {
        return hashTable.entrySet();
    }
   
    // Return true if hashTable equals to the given Object
    public boolean equals(Object obj)
    {  
        return hashTable.equals(obj);
    }
   
    // Returns the value to which the specified key is mapped, 
    // or null if the map contains no mapping for the key.
    public V get(Object key)
    {
        return hashTable.get(key);
    }
   
    // Returns the hash code value for the Map 
    public int hashCode()
    {
        return hashTable.hashCode();
    }
   
    // Check whether hashTable is empty or not
    public boolean isEmpty()
    {
        return hashTable.isEmpty();
    }
   
    // Returns an enumeration of the keys in on the hashtable
    public Enumeration<K> keys()
    {
        return hashTable.keys();
    }
   
    // Returns a Set view of the keys
    public Set<K> keySet()
    {
        return hashTable.keySet();
    }
   
    // Maps the specified key to the specified value in this hashTable
    public V put(K key, V val)
    {
        return hashTable.put(key, val);
    }
   
    // Returns the number of keys in the hashtable
    public int size()
    {
        return hashTable.size();
    }
   
    //Returns a string representation of the Hashtable objects in the form of String
    public String toString()
    {
        return hashTable.toString();
    }
   
    // Removes the given key with the value from the hashTable
    public V remove(Object key)
    {
        return hashTable.remove(key);
    }
   
    // Returns a Collection view of the values
    public Collection<V> values()
    {
        return hashTable.values();
    }
}
public class GFG{ 
    public static void main(String[] arg)
    {
        HashTableImplementation<String, Integer> hashTable = 
                         new HashTableImplementation<>();
          
        // Add elements to hashTable
        hashTable.put("Nikhil", 390);
        hashTable.put("Akshay", 280);
        hashTable.put("Bina", 500);
        hashTable.put("Chintu", 450);
          
        // Print the size of the hashTable
        System.out.println("The size of the hashTable: " + hashTable.size());
          
        // Iterate and print the EntrySet of the hashTable
        System.out.println("Entry Set of the hashTable: ");
        Set<Entry<String, Integer>> entrySet = hashTable.entrySet();
        Iterator<Entry<String, Integer>> entry = entrySet.iterator();
        
        while (entry.hasNext())
        {
            System.out.println(entry.next() + " ");
        }
        System.out.println();
          
        // Iterate and print the keys of the hashTable
        System.out.println("The keys of the HashTable: ");
        
        Set<String> keySet = hashTable.keySet();
        
        Iterator<String> it = keySet.iterator();
        
        while (it.hasNext())
        {
            System.out.print(it.next() + " ");
        }
        System.out.println();
          
        // Iterate and print the value of the hashTable
        System.out.println("The values of the HashTable:");
        
        Collection<Integer> values = hashTable.values();
        
        Iterator<Integer> itr = values.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + " ");
        }
        System.out.println();
          
        // Print true if hashTable contains the key "Nikhil" 
        System.out.println("The hashTable contains Nikhil: "
                           + hashTable.containsKey("Nikhil"));
          
        // Delete all the entrys
        hashTable.clear();
          
        // Print size of the hashTable
        System.out.println("The size of the hashTable: " 
                           + hashTable.size());
          
    }
}


Output

The size of the hashTable: 4
Entry Set of the hashTable: 
Chintu=450 
Nikhil=390 
Akshay=280 
Bina=500 

The keys of the HashTable: 
Chintu Nikhil Akshay Bina 
The values of the HashTable:
450 390 280 500 
The hashTable contains Nikhil: true
The size of the hashTable: 0

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments