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 Escaping Characters in Java RegExps
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Java Program to Implement Find all Forward Edges in a Graph
Using Java Assertions
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Disable DNS caching
How to Read a Large File Efficiently with Java
Guide to BufferedReader
Pagination and Sorting using Spring Data JPA
Working With Maps Using Streams
Guava – Join and Split Collections
Java Program to Implement Bresenham Line Algorithm
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Represent Graph Using Adjacency Matrix
Spring Security 5 – OAuth2 Login
Creating a Custom Starter with Spring Boot
Java Program to Compute Cross Product of Two Vectors
Java Program to Implement Sorted Doubly Linked List
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Spring Boot - Zuul Proxy Server and Routing
Java Program to Implement a Binary Search Tree using Linked Lists
Object Type Casting in Java
How to Read HTTP Headers in Spring REST Controllers
Spring Boot - Admin Client
Một số nguyên tắc, định luật trong lập trình
Tính đa hình (Polymorphism) trong Java
Java Program to Implement Gauss Seidel Method
Guide to Guava Table
Tổng quan về ngôn ngữ lập trình java
How to use the Spring FactoryBean?