Java Program to Implement HashTable API

This Java program is to Implement HashTable Collection API. 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. The initial capacity and load factor parameters are merely hints to the implementation. The exact details as to when and whether the rehash method is invoked are implementation-dependent.

Here is the source code of the Java program to Implement HashTable Collection API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
public class HashTableImpl<K, V>
{
    private Hashtable<K, V> hashTable;
 
    /*
     * Constructs a new, empty hashtable with a default initial capacity (11)
     * and load factor (0.75).
     */
    public HashTableImpl()
    {
        hashTable = new Hashtable<K, V>();
    }
 
    /*
     * Constructs a new, empty hashtable with the specified initial capacity and
     * default load factor (0.75).
     */
    public HashTableImpl(int initialCapacity)
    {
        hashTable = new Hashtable<K, V>(initialCapacity);
    }
 
    /*
     * Constructs a new, empty hashtable with the specified initial capacity and
     * the specified load factor.
     */
    public HashTableImpl(int initialCapacity, float loadFactor)
    {
        hashTable = new Hashtable<K, V>(initialCapacity, loadFactor);
    }
 
    /* Constructs a new hashtable with the same mappings as the given Map. */
    public HashTableImpl(Map<? extends K, ? extends V> t)
    {
        hashTable = new Hashtable<K, V>(t);
    }
 
    /* Clears this hashtable so that it contains no keys. */
    public void clear()
    {
        hashTable.clear();
    }
 
    /* Creates a shallow copy of this hashtable. */
    public Object clone()
    {
        return hashTable.clone();
    }
 
    /* Tests if some key maps into the specified value in this hashtable. */
    public boolean contains(Object value)
    {
        return hashTable.contains(value);
    }
 
    /* Returns true if this hashtable maps one or more keys to this value. */
    public boolean containsValue(Object value)
    {
        return hashTable.containsValue(value);
    }
 
    /* Tests if the specified object is a key in this hashtable. */
    public boolean containsKey(Object key)
    {
        return hashTable.containsKey(key);
    }
 
    /* Returns an enumeration of the values in this hashtable. */
    public Enumeration<V> elements()
    {
        return hashTable.elements();
    }
 
    /* Returns an enumeration of the values in this hashtable. */
    public Set<Map.Entry<K, V>> entrySet()
    {
        return hashTable.entrySet();
    }
 
    /*
     * Compares the specified Object with this Map for equality, as per the
     * definition in the Map interface.
     */
    public boolean equals(Object o)
    {  
        return hashTable.equals(o);
    }
 
    /*
     * Returns the value to which the specified key is mapped, or null if this
     * map contains no mapping for the key.
     */
    public V get(Object key)
    {
        return hashTable.get(key);
    }
 
    /*
     * Returns the hash code value for this Map as per the definition in the Map
     * interface.
     */
    public int hashCode()
    {
        return hashTable.hashCode();
    }
 
    /* Tests if this hashtable maps no keys to values. */
    public boolean isEmpty()
    {
        return hashTable.isEmpty();
    }
 
    /* Returns an enumeration of the keys in this hashtable. */
    public Enumeration<K> keys()
    {
        return hashTable.keys();
    }
 
    /* Returns a Set view of the keys contained in this map. */
    public Set<K> keySet()
    {
        return hashTable.keySet();
    }
 
    /* Maps the specified key to the specified value in this hashtable. */
    public V put(K key, V value)
    {
        return hashTable.put(key, value);
    }
 
    /* Returns the number of keys in this hashtable. */
    public int size()
    {
        return hashTable.size();
    }
 
    /*
     * Returns a string representation of this Hashtable object in the form of a
     * set of entries, enclosed in braces and separated by the ASCII characters
     * ", " (comma and space).
     */
    public String toString()
    {
        return hashTable.toString();
    }
 
    /* Removes the key (and its corresponding value) from this hashtable. */
    public V remove(Object key)
    {
        return hashTable.remove(key);
    }
 
    /* Returns a Collection view of the values contained in this map. */
    public Collection<V> values()
    {
        return hashTable.values();
    }
 
    public static void main(String... arg)
    {
        HashTableImpl<Integer, Integer> hashTable = new HashTableImpl<Integer, Integer>();
 
        hashTable.put(1, 100);
        hashTable.put(2, 200);
        hashTable.put(3, 300);
        hashTable.put(4, 100);
 
        System.out.println("the key set of the hashTable is ");
        Set<Integer> keySet = hashTable.keySet();
        Iterator<Integer> itr = keySet.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the values of the hashTable is ");
        Collection<Integer> values = hashTable.values();
        itr = values.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
        System.out.println("the entry set of the hash table is");
        Set<Entry<Integer, Integer>> entrySet = hashTable.entrySet();
        Iterator<Entry<Integer, Integer>> eitr = entrySet.iterator();
        while (eitr.hasNext())
        {
            System.out.println(eitr.next() + "\t");
        }
        System.out.println("the enumeration of the elements in hash Table");
        Enumeration<Integer> enumeration = hashTable.elements();
        while (enumeration.hasMoreElements())
        {
            System.out.print(enumeration.nextElement() + "\t");
        }
        System.out.println();
 
        System.out.println("the enumeration of the keys in hash Table");
        enumeration = hashTable.keys();
        while (enumeration.hasMoreElements())
        {
            System.out.print(enumeration.nextElement() + "\t");
        }
        System.out.println();
        System.out.println("the value " + hashTable.remove(2) + "removed");
        System.out.println("the hash table contains 200 " + hashTable.containsValue(200));
        hashTable.clear();
        if (hashTable.isEmpty())
            System.out.println("the hash table is empty after clear");
        else
            System.out.println("the hash table is not empty after clear");
    }
}
$javac HashTableImpl.java
$java HashTableImpl
the key set of the hashTable is 
4	3	2	1	
the values of the hashTable is 
100	300	200	100	
the entry set of the hash table is
4=100	
3=300	
2=200	
1=100	
the enumeration of the elements in hash Table
100	300	200	100	
the enumeration of the keys in hash Table
4	3	2	1	
the value 200removed
the hash table contains 200 false
the hash table is empty after clear

Related posts:

Guide to the Java Queue Interface
Circular Dependencies in Spring
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to Implement Sorted Circular Doubly Linked List
Java Program to Implement Shoelace Algorithm
Why String is Immutable in Java?
Logging in Spring Boot
Java Program to Implement Splay Tree
Java Program to Implement Cartesian Tree
Spring Boot - Enabling Swagger2
JUnit5 @RunWith
Spring Security OAuth Login with WebFlux
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Java Program to Find Number of Articulation points in a Graph
Giới thiệu java.io.tmpdir
How to use the Spring FactoryBean?
Java Program to Perform Complex Number Multiplication
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
New Features in Java 14
Java Program to Perform Naive String Matching
Java CyclicBarrier vs CountDownLatch
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Check whether Undirected Graph is Connected using BFS
Tổng quan về ngôn ngữ lập trình java
Java Program to Implement Gaussian Elimination Algorithm
Converting String to Stream of chars
Introduction to Netflix Archaius with Spring Cloud
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Receive email using IMAP