Java Program to Implement HashTable API Last Updated : 22 Feb, 2021 Comments Improve Suggest changes Like Article Like Report 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()); } } OutputThe 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 Comment More infoAdvertise with us Next Article Java Program to Implement HashTable API N nikhilchhipa9 Follow Improve Article Tags : Java Java Programs Java-Collections Java-HashTable Practice Tags : JavaJava-Collections Similar Reads Java Program to Implement IdentityHashMap API The IdentityHashMap implements Map interface using Hashtable, using reference-equality in place of object-equality when comparing keys (and values). This class is not a general-purpose Map implementation. While this class implements the Map interface, it intentionally violates Mapâs general contract 6 min read Java Program to Implement ConcurrentHashMap API ConcurrentHashMap class obeys the same functional specification as HashTable and includes all the versions of methods corresponding to each method of a HashTable. A HashTable supports the full concurrency of retrievals and adjustable concurrency for updates. All the operations of ConcurrentHashMap a 6 min read Java Program to Implement HashTables with Linear Probing Hashing is a technique that is used to uniquely identify a specific object from a group of similar objects. Suppose an object is to be assigned a key to it to make searching easy. To store the key/value pair, one can use a simple array like a data structure where keys (integers) can be used directly 5 min read Java Program to Implement ConcurrentSkipListMap API ConcurrentSkipListMap API is based on the implementation of the ConcurrentNavigableMap. This map is sorted according to the natural ordering of its keys or by a Comparator provided on the map during creation time. This class implements a concurrent variant of SkipLists which makes its expected avera 10 min read Java Program to Implement LinkedHashMap API The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements 3 min read Java Program to Implement LinkedHashSet API The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained, this class is used. When iterating through a HashSet, the order is unpredictable, while a LinkedHashSet lets us iterate through the element 4 min read Java Program to Implement Hash Table Chaining with List Heads A hash table is a data structure that executes a connected array, it is a structure that maps keys to its values. It uses a hash function to calculate an index position for the keys, also called hash code. It will store key and value pair in an array of buckets, and from that bucket, the values can 4 min read Java Program to Implement Hash Tables with Double Hashing Double hashing is a technique in an open addressing scheme. and there is the ordinary hash function. In an open addressing scheme, the actual hash function is taking the ordinary hash function when its space is not empty then it will perform another hash function to get some space to insert. Double 10 min read How to Iterate Through HashTable in Java? HashTable is an underlying data structure where the insertion order in HashTable is not preserved, and it is based on the hashcode of keys. Duplicates keys are not allowed, but values can be duplicated. Heterogeneous objects are allowed for both keys and values. Value null is not allowed for both ke 8 min read Java Program to Read Elements using Enumeration in Hashtable The enumeration in java is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable(like Stack, Vector, HashTable, etc.) in a forward direction only and not in the backward direction. HashTable is a class The hash table class implements a Map 3 min read Like