Java

Java – Hashtable With Example

This class provides a hash table that associates values with keys. You may use any non-null object as a key or a value. Hashtable and HashMap are comparable, but Hashtable is synchronized. There are a few additional distinctions between the HashMap and Hashtable classes, which you can learn about in more depth at: HashMap and Hashtable Differences.

In this article, we’ll learn how to establish a hashtable, how to add entries to it, and how to use enumeration to show the key-value pairs in the hashtable. We shall examine Hashtable tutorials and methods of the Hashtable class at the conclusion of this post.

Example

import java.util.Hashtable;
import java.util.Enumeration;

public class HashtableExample {
 
 public static void main(String[] args) {
 
   Enumeration names;
   String key;
 
   // Creating a Hashtable
   Hashtable<String, String> hashtable = 
              new Hashtable<String, String>();
 
   // Adding Key and Value pairs to Hashtable
   hashtable.put("Key1","Chaitanya");
   hashtable.put("Key2","Ajeet");
   hashtable.put("Key3","Peter");
   hashtable.put("Key4","Ricky");
   hashtable.put("Key5","Mona");
 
   names = hashtable.keys();
   while(names.hasMoreElements()) {
      key = (String) names.nextElement();
      System.out.println("Key: " +key+ " & Value: " +
      hashtable.get(key));
   }
 }
}

Output:

Key: Key4 & Value: Ricky
Key: Key3 & Value: Peter
Key: Key2 & Value: Ajeet
Key: Key1 & Value: Chaitanya
Key: Key5 & Value: Mona

Hashtable tutorials

  • Hashtable example
  • Sort Hashtable
  • Hashtable Iterator example
  • Check key-value existence in Hashtable
  • Remove mapping from Hashtable
  • Remove all mappings from Hashtable
  • Get size of Hashtable
  • Hashtable vs HashMap

Methods of Hashtable class:

1) void clear(): Empties the Hashtable by removing all the key-value mappings. This hashtable is cleared to remove all keys.

2) Object clone(): Produces a lightweight duplicate of this hashtable. The keys and values are not duplicated, just the whole hashtable’s structure. This procedure is somewhat pricey.

3) boolean contains(Object value): Determines if a key in this hashtable corresponds to the given value. The containsKey technique is less costly than this procedure.
Keep in mind that the functionality of this method and that of containsValue (which is part of the Map interface in the collections framework).

4) The boolean function isEmpty() determines if this hashtable translates no keys to values.

5) Enumeration keys(): This function returns a list of all the keys in the hash table.

6) Object put(Object key, Object value): This hashtable’s object put function maps the supplied key to the specified value.

7) void rehash(): Expands the hash table’s size and rehashes each key.

8) Object remove(Object key): Eliminates the specified key (along with its related value) from the hashtable.

9) int size(): This function returns the total number of key-value mappings in the Hashtable.

The string equivalent of a hash table is produced by the function string toString().

The eleventh function, boolean containsKey(Object key), determines if the given object is a key in this hashtable.

12) boolean containsValue(Object value): Checks to see whether the given object is one of the values in this hashtable. If a value matching value exists in the hash table, the function returns true. If the value cannot be found, returns false.

13) Enumeration elements(): This function returns a list of all the values in the hash table.

14) Object get(Object key): Returns the value that corresponds to the key supplied, or null if the key is not mapped to a value in this map.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button