Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Hashtable

Hashtable is a class available in the java.util package. It extends the java.util.Dictionary class and implements the java.util.Map, java.util.Cloneable, and java.io.Serializable interfaces.

Important points of java.util.Hashtable class:

1. It is a legacy class that has been re-engineered and included in the collection framework.
2. It stores elements in Key-Value format.
3. We can store different types of Key objects and value objects.
4. We can't store null values as keys and values.
5. It uses an index representation to store its elements.
6. Random access is allowed in Hashtable.
7. Methods of the java.util.Hashtable class are synchronized in nature.
8. It stores elements in an unordered way.
9. We can't iterate over Hashtable using Enumeration.

Important Constructor of java.util.Hashtable:

1. public java.util.Hashtable(): It creates an empty Hashtable with the default capacity (11) and load factor (0.75).

2. public java.util.Hashtable(int): It creates an empty Hashtable with the specified capacity and default load factor (0.75). It throws IllegalArgumentException when a negative capacity is passed.

3. public java.util.Hashtable(int, float): It creates an empty Hashtable with the specified capacity and load factor. It throws IllegalArgumentException when a negative capacity or load factor is passed.

4. public java.util.Hashtable(java.util.Map): It creates a Hashtable with the same mappings as the specified Map, and the capacity will be sufficient to hold the mappings of the specified Map. The load factor is set to 0.75. It throws a NullPointerException when the specified Map is null.

Important Method of java.util.Hashtable class:

1. public synchronized int size(): It returns the number of keys in the current working Hashtable.

2. public synchronized boolean isEmpty(): It returns true when the Hashtable has no key otherwise returns false.

3. public synchronized java.util.Enumeration keys(): Returns an enumeration of the keys in this hashtable. Use the methods provided by Enumeration on the returned object to sequentially fetch the keys. If the hashtable undergoes structural modifications while enumerating the keys, the results are undefined.

4. public synchronized java.util.Enumeration elements(): Returns an enumeration of the values in this hashtable. Use the methods provided by Enumeration on the returned object to sequentially fetch the elements. If the hashtable undergoes structural modifications while enumerating the values, the results are undefined.

5. public synchronized boolean contains(java.lang.Object): This method returns true if the specified value is part of the mapping in the current Hashtable; otherwise, it returns false. It throws a NullPointerException if the specified value is null.

6. public synchronized boolean containsKey(java.lang.Object): It returns true if the specified key is present in the java.util.Hashtable; otherwise, it returns false. It throws a NullPointerException when a null value is passed as an argument.

7. public synchronized Object get(java.lang.Object): It returns the value object mapped with the specified key object from the current working Hashtable; otherwise, it returns null. It throws a NullPointerException when a null value is passed as the key.

8. public synchronized Object put(Object Key, Object Value): It maps the value to the specified key in the current working Hashtable and returns the previous value associated with the specified key. If there is no existing mapping, it returns null.

9. public synchronized Object remove(java.lang.Object): It removes the key from the Hashtable and returns its mapped value. If there is no mapping associated with the specified key, it returns null.

10. public synchronized void putAll(java.util.Map): Copies all of the mappings from the specified map to this hashtable. These mappings will replace any existing mappings for keys present in the specified map within this hashtable.

11. public synchronized void clear(): It clears the current Hashtable.

12. public synchronized Object getOrDefault(java.lang.Object Key, java.lang.Object default_value): It returns the value that is mapped with the specified Key-Object, and if there is no mapping for the specified key, it returns the specified default value.

            
import java.util.Enumeration;
import java.util.Hashtable;
importjava.util.TreeMap;
importjavax.annotation.processing.SupportedSourceVersion;
publicclass JTC {
   publicstaticvoid main(String[] args) {
      // Creating empty Hashtable using default constrcutor.
      Hashtablehashtable = newHashtable();
      // Adding elements into Hashtable using put() methood.
      System.out.println(hashtable.put("K-1", "V-1"));
      System.out.println(hashtable.put(101, true));
      System.out.println(hashtable.put((byte) 78, 12.34 f));
      System.out.println(hashtable.put('A', new JTC()));
      System.out.println(hashtable.put(new JTC(), 'V'));
      System.out.println(hashtable.put((byte) 78, 'N'));
      // System.out.println(hashtable.put(null, "JTC")); ---> NullPointerException
      // System.out.println(hashtable.put("Hello", null)); ---> NullPointerException
      System.out.println("hashtable :- " + hashtable);
      // Getting number of elements using size().
      System.out.println("size :- " + hashtable.size());
      // Getting Empty status using isEmpty().
      System.out.println("Empty status :- " + hashtable.isEmpty());
      // Getting Keys Enumeration using keys().
      System.out.println("-----Keys-----");
      Enumerationenumeration1 = hashtable.keys();
      while (enumeration1.hasMoreElements()) {
         System.out.println(enumeration1.nextElement());
      }
      // Getting Values Enumeration using elements().
      System.out.println("----Values-----");
      Enumerationenumeration2 = hashtable.elements();
      while (enumeration2.hasMoreElements()) {
         System.out.println(enumeration2.nextElement());
      }
      // Checking the specified value is mapped in current Hashtable or not using
      // contains().
      System.out.println(hashtable.contains("V-1"));
      System.out.println(hashtable.contains("K-1"));
      // System.out.println(hashtable.contains(null)); ---> NullPointerException
      // Check the specified key is mapped in current Hashtable or not using
      // containsKeys().
      System.out.println(hashtable.containsKey("K-1"));
      System.out.println(hashtable.containsKey("V-1"));
      // System.out.println(hashtable.containsKey(null)); ---> NullPointerException
      // Getting Value associated with specified key.
      System.out.println(hashtable.get("K-1"));
      System.out.println(hashtable.get("V-1"));
      // System.out.println(hashtable.get(null)); ---> NullPointerException
      // Removing mapping from the current working Hashtable using remove().
      System.out.println("Before Remove Operation :- " + hashtable);
      System.out.println("Removed Mapping Value is :- " + hashtable.remove("K-1"));
      System.out.println("After Remove Operation :- " + hashtable);
      System.out.println(hashtable.remove("V-1"));
      // System.out.println(hashtable.remove(null)); ---> NullPointerException
      // Adding all the mapping of the specified hashtable into the current working hashtable using putAll().
      Hashtablehashtable2 = newHashtable();
      hashtable2.put('A', "Value - 1");
      hashtable2.put("Key-2", "Value-2");
      hashtable.put("Key-3", "Value-3");
      System.out.println("hashtable2 :- " + hashtable2);
      hashtable.putAll(hashtable2);
      System.out.println("hashtable :- " + hashtable);
      // hashtable.putAll(null); ---> NullPointerException
      // Clear current working hashtable using clear();
      System.out.println("Before clear operation size of hashtable2 is :- " + hashtable2.size());
      hashtable2.clear();
      System.out.println("After clear operation size of hashtable2 is :- " + hashtable2.size());
      // Getting value mapped with specified Key or Default_Value using
      // getOrDefault().
      System.out.println(hashtable.getOrDefault("Key-2", "DEFAULT_VALUE"));
      System.out.println(hashtable.getOrDefault("Key-N", "DEFAULT_VALUE"));
   }
}
   null
   null
   null
   null
   null
   12.34
   hashtable :- {A=com.P1.JTC@4926097b, com.P1.JTC@5e91993f=V, K-1=V-1, 101=true, 78=N}
   size :- 5
   Empty status :- false
   -----Keys-----
   A
   com.P1.JTC@5e91993f
   K-1
   101
   78
   ----Values-----
   com.P1.JTC@4926097b
   V
   V-1
   true
   N
   true
   false
   true
   false
   V-1
   null
   Before Remove Operation :- {A=com.P1.JTC@4926097b, com.P1.JTC@5e91993f=V, K-1=V-1, 101=true, 78=N}
   Removed Mapping Value is :- V-1
   After Remove Operation :- {A=com.P1.JTC@4926097b, com.P1.JTC@5e91993f=V, 101=true, 78=N}
   null
   hashtable2 :- {A=Value - 1, Key-2=Value-2}
   hashtable :- {A=Value - 1, com.P1.JTC@5e91993f=V, 101=true, Key-3=Value-3, 78=N, Key-2=Value-2}
   Before clear operation size of hashtable2 is :- 2
   After clear operation size of hashtable2 is :- 0
   Value-2
   DEFAULT_VALUE
            

In this example we are looking for all the important methods implementation of java.util.Hashtable class.