Author
Pavlo Plynko
Java Developer at CodeGym

Java Hashtable

Published in the Java Collections group
Java Hashtable class is one of the oldest members of Java Collection Framework. It is an implementation of mathematical hash table data structure. In Java hashtable internally contains buckets where the key/value pairs are stored. Hashtable is pretty similar to HashMap. The most significant difference between them: Hashtable is synchronized while HashMap is not.

Hashtable as a data structure

Hashtable is a data structure where data is stored in an array format. Every data value has a unique key value. If the key is known, access to the needed data is very fast. So, insertion and search operations are fast independently on the data size. Hash Table consists of an array to keep data and hashing for generation an index where an element should be located. What is hashing? It is a rule that maps the Object into a set of characters (code). Usually that kind of function converts a big piece of data into a small integer value. Hash functions could be different, but they all submit certain properties:
  • A particular object has the particular hash code.
  • Two equal objects have the same hash codes. The reverse is not true.
  • If two hash codes are different, the objects are definitely not equal.
  • Different objects may have the same hash code. This very rare event calls collision. The good hash function minimizes probability of collisions.
The result of applying Hash Function to an Object calls hashCode.

Hashtable in Java

Hashtable class is the implementation of a hash table data structure. This collection was created earlier than the Java Collection Framework, but was later included in it. Like all “early” collections (from Java 1.0), a hashtable is synchronized (almost all methods are marked as synchronized). Because of this factor, hashtable has significant performance problems. Hence, starting from Java 1.2, in most cases it is recommended to use other implementations of the Map interface due to their lack of synchronization. Usually HashMap is the most appropriate replacement. So Class Hashtable<K,V> consists of keys and values. It stores keys on the principle of hashing. Key-value pairs are stored in "buckets". The buckets together construct a "table", a kind of internal array. Hashtable uses the key’s hashcode to determine a bucket where the key/value pair should map. Hash function lets to get bucket location from Key’s hashcode. This function returns an integer number for an object. As we said above two equal objects have the same hashcode, while two unequal objects might not always have different hashcodes. Different objects, put into a hashtable may have the same hash code. To resolve this problem (collision) array of lists are used in hashtable. The pairs mapped to a single bucket are stored in a list and this list reference is stored in array index.

Hashtable Java Constructors

  • Hashtable(), the default constructor. It creates an empty hashtable. (Default initial capacity = 11, load factor =0.75).
  • Hashtable(int size) constructs a hashtable of specified size.
  • Hashtable(int size, float fillRatio) creates hash table of specified size and fill ratio.
  • Hashtable(Map m) creates a hashtable with the same mappings as the given Map.

Hashtable Declaration

The Hashtable Java class implements Map, Cloneable and Serializable interfaces. It extends Dictionary class.

Hashtable.java
public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable
K is the type of keys maintained by the map. V is the type of mapped values. Example:

Hashtable<Student, Integer> myHTable = new Hashtable<>();

How to import hashtable java

Java Hashtable is inside java.util package. So use import java.util.Hashtable; in your code. Usually you will get a hint from your IDE about this.

Hashtable main operations

The main operations of the Hashtable is getting, insertion to the collection and removing from there. Here these three operations are:
  • Object get(Object key) returns the value of the Object that has specified key. Returns null if no such key is found.
  • Object put(Object key, Object value) maps the specified key to the specified value. Neither the key nor the value can be null.
  • Object remove(Object key) removes the entry (key and corresponding value) from hashtable.
The other important operations:
  • int size() returns the quantity of entries in the hash table.
  • boolean contains(Object value) checks if specified value is in the hash table. If so, method returns true, else returns false.
  • boolean containsValue(Object value) checks if specified value is in the hash table. If so, method returns true, else returns false.
  • void clear() removes all entries from the hashtable.
  • boolean containsKey(Object key) returns true if specified key exists in the hash table, else return false.
  • boolean isEmpty() returns true if the hashtable is empty or false if it contains at least one key.
  • void rehash() increases the size of the hashtable and rehashes all of its keys.

Hash table implementation, Java code:

Let’s create a Student class:

import java.util.Date;
public class Student {
   String surname;
   String name;
   String secondName;
   Long birthday; // Long instead of long is used by Gson/Jackson json parsers and various orm databases

   public Student(String surname, String name, String secondName, Date birthday ){
       this.surname = surname;
       this.name = name;
       this.secondName = secondName;
       this.birthday = birthday == null ? 0 : birthday.getTime();
   }

   @Override
   public int hashCode(){
       //TODO: check for nulls
       return (surname + name + secondName + birthday).hashCode();
   }
   @Override
   public boolean equals(Object other_) {
       Student other = (Student)other_;
       return (surname == null || surname.equals(other.surname) )
               && (name == null || name.equals(other.name))
               && (secondName == null || secondName.equals(other.secondName))
               && (birthday == null || birthday.equals(other.birthday));
   }
}
Here is Java Hashtable example. Let’s put two Objects of the Student class into hashtable, then remove some and check some parameters.

public class HashTableExample {
   public static void main(String[] args) {
 
       Hashtable<Student, Integer> myHTable = new Hashtable<>();
       Student sarah1 = new Student("Sarah","Connor", "Jane", null);
       Student john = new Student("John","Connor", "Kyle", new Date(1985, 02-1, 28)); // date not exists
       myHTable.put(john,1);
       myHTable.put(sarah1,0);
       System.out.println(myHTable.get(john));
       System.out.println(myHTable.isEmpty());
       System.out.println(myHTable.size());
       System.out.println(myHTable.contains(1));
       myHTable.remove(john);
       System.out.println(myHTable.contains(0));
       System.out.println(myHTable.contains(1));
       System.out.println(myHTable.containsKey(sarah1));
   }
}
The result of running program is:

1
false
2
true
true
false
true

HashMap vs Hashtable

  • Hashtable is similar to HashMap in Java. The most significant difference is that Hashtable is synchronized while HashMap is not. Therefore, Hashtable is slower than HashMap because of synchronization.
  • Except of synchronization problem, Hashtable does not allow null to be used as a value or key. HashMap allows one null key and multiple null values.
  • Hashtable inherits Dictionary class while HashMap inherits AbstractMap class.
  • HashMap is traversed by Iterator. Hashtable can be traversed not only by Iterator but also by Enumerator.

Java hashtable example (Hashtable vs HashMap null key)

Here is a fragment code to demonstrate a null used as a key and value in HashMap and Hashtable

// Null key Java hashtable example and hashmap example  

try{
      System.out.println("Hashtable");
      Hashtable hashTable = new Hashtable();
      hashTable.put(null, new Object());
    }catch(Exception ex){
      ex.printStackTrace();
    }
    System.out.println("HashMap");
    HashMap hashMap = new HashMap();
    hashMap.put(null, new Object());
    System.out.println("as you see no exceptions with null key in HashMap");
  }
The result of running the program that contains this fragment:

java.lang.NullPointerException
	at java.base/java.util.Hashtable.put(Hashtable.java:480)
	at Character.main(Character.java:58)
HashMap
as you see no exceptions with null key in HashMap

Conclusion

You don’t really often will use Hashtable in real projects, but it is easy to meet this data structure in old projects. Anyway, it is important to understand what Data Structures Java has and how they work, at least for your interviews.Usually HashMap objects are used instead of Hashtable because of their similarity. HashMap is more effective (it is not synchronized) and could have null as a key.
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Ri Hab Level 0
12 October 2020
then thank u 😎😎🤩🤩🤩🥰🥰