Constructors and methods of Hashtable in Java

There are four constructors defined for Hashtable.

  • Hashtable()
  • Hashtable(int initialCapacity)
  • Hashtable(int initialCapacity, float loadFactor)
  • Hashtable(Map map)

Hashtable() – This is default constructor which is used to create new and empty Hashtable with initial capacity 16 and the load factor .75. Inside this constructor, we initialize the load factor with its default value i.e 0.75f  Let’s see the internal code of this constructor.

 public Hashtable() {
        this(11, 0.75f);
    }

Example –

package hashtableconstructor;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
public class Example1 {
public static void main(String[] args) {
	
	//creating Hashtable object using default constructor
	Map<Integer, String> hashtableObject = new Hashtable<>();
	 
	hashtableObject.put(2, "ram");
	hashtableObject.put(8, "mohan");
	hashtableObject.put(3, "sohan");
	hashtableObject.put(5, "suresh");

	// we will iterate hashtable using Iterator
	Iterator<Map.Entry<Integer, String>> iterator = hashtableObject
			.entrySet().iterator();

	while (iterator.hasNext()) {
		Map.Entry<Integer, String> entry = iterator.next();
		System.out.println("Key is = " + entry.getKey() + ", Value is = "
				+ entry.getValue());
	}
}
}

Output is –

Key is = 8, Value is = mohan
Key is = 5, Value is = suresh
Key is = 3, Value is = sohan
Key is = 2, Value is = ram

Hashtable(int initialCapacity) –  We can create a new Hashtable defining some different capacity than 16 using this constructor(default factor still will be 0.75f).

Internal implementation-

 public Hashtable(int initialCapacity) {
        this(initialCapacity, 0.75f);
    }

Example –

package hashtableconstructor;

import java.util.*;
import java.util.Iterator;
import java.util.Map;
 
public class Example2 {
public static void main(String[] args) {
	
	//creating Hashtable object using initial capacity 20
	Map<Integer, String> hashtableObject = new Hashtable<>(20);
	 
	hashtableObject.put(2, "ram");
	hashtableObject.put(8, "mohan");
	hashtableObject.put(4, "Bangalore");
	hashtableObject.put(5, "Rakesh");
 
	// we will iterate using Iterator
	Iterator<Map.Entry<Integer, String>> iterator = hashtableObject
			.entrySet().iterator();
 
	while (iterator.hasNext()) {
		Map.Entry<Integer, String> entry = iterator.next();
		System.out.println("Key is = " + entry.getKey() + ", Value is = "
				+ entry.getValue());
	}
}
}

Output is –

Key is = 8, Value is = mohan
Key is = 5, Value is = Rakesh
Key is = 4, Value is = Bangalore
Key is = 2, Value is = ram

Hashtable(int initialCapacity, float loadFactor) – We can also create a new Hashtable with new capacity as well as new load factor, rather than 16 and 0.75.

Internal implementation-

public Hashtable(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        table = new Entry<?,?>[initialCapacity];
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    }

Example –

package hashtableconstructor;

import java.util.*;
import java.util.Iterator;
import java.util.Map;
 
public class Example3 {
public static void main(String[] args) {
	
	//creating Hashtable object using initial capacity 20 and loadfactor 0.80f
	Map<Integer, String> hashtableObject = new Hashtable<>(20,0.80f);
	 
	hashtableObject.put(2, "ram");
	hashtableObject.put(8, "mohan");
	hashtableObject.put(4, "Bangalore");
	hashtableObject.put(5, "Rakesh");
 
	// we will iterate using Iterator
	Iterator<Map.Entry<Integer, String>> iterator = hashtableObject
			.entrySet().iterator();
 
	while (iterator.hasNext()) {
		Map.Entry<Integer, String> entry = iterator.next();
		System.out.println("Key is = " + entry.getKey() + ", Value is = "
				+ entry.getValue());
	}
}
}

Output –

Key is = 8, Value is = mohan
Key is = 5, Value is = Rakesh
Key is = 4, Value is = Bangalore
Key is = 2, Value is = ram

Hashtable(Map map) – We can pass any type of classes which implements Map interface and can convert it into Hashtable. For example, we are going to convert HashMap into Hashtable in below example before that let’s see how this constructor has been defined in Hashtable class.

Internal implementation –

    public Hashtable(Map<? extends K, ? extends V> t) {
        this(Math.max(2*t.size(), 11), 0.75f);
        putAll(t);
    }

Example –

package hashtableconstructor;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

public class Example4 {
public static void main(String[] args) {
	
	     //creating HashMap object using, later we will convert into Hashtable
		Map<Integer, String> hashmapObject = new HashMap<>();
		 
		hashmapObject.put(2, "ram");
		hashmapObject.put(8, "mohan");
		hashmapObject.put(4, "Bangalore");
		hashmapObject.put(5, "Rakesh");
		

		// we will convert Hashtable into HashMap using below constructor
		Hashtable<Integer,String> hashtableObj = new Hashtable<>(hashmapObject);
		Iterator<Map.Entry<Integer, String>> iterator = hashtableObj
				.entrySet().iterator();

		while (iterator.hasNext()) {
			Map.Entry<Integer, String> entry = iterator.next();
			System.out.println("Key is = " + entry.getKey() + ", Value is = "
					+ entry.getValue());
		}
}
}

Output is –

Key is = 8, Value is = mohan
Key is = 5, Value is = Rakesh
Key is = 4, Value is = Bangalore
Key is = 2, Value is = ram

 

For method details refer to documents.