How to create Hashtable program in java

import java.util.*;

class Hashtable_TechSarvam 
{
    public static void main(String args[]) 
    {
        // Creating a Hashtable
        Hashtable<Integer, String> hm = new Hashtable<Integer, String>();

        // Adding elements to the Hashtable
        hm.put(100, "Rajendra");
        hm.put(102, "Praveen");
        hm.put(101, "Bipin");
        hm.put(103, "Pankaj");

        // Iterating through the Hashtable
        for (Map.Entry<Integer, String> m : hm.entrySet()) 
        {
            System.out.println(m.getKey() + " " + m.getValue());
        }
    }
}

Step-by-Step Explanation:

Let’s go through the code step by step:

1. Importing Required Classes:

Statement in Interpreter

import java.util.*;

Package imported: java.util.* This package contains classes and interfaces for the following utilities: Hashtable, Map.Entry, Iterator, etc. are used in a program.

2. Class Declaration:

class Hashtable_TechSarvam {

A class is defined as Hashtable_TechSarvam. This class contains the main() method, which is the program’s entry point.

3. Main Method:

public static void main(String args[]) {

The main() method is the starting point of the Java program. The code inside this method is executed when the program is run.

4. Creating a Hashtable Instance:

Hashtable<Integer, String> hm = new Hashtable<Integer, String>();

  • A Hashtable instance by the name of hm. The Hashtable is an implementation class to the Map interface storing key-value pair.
  • The map is parameterized with <Integer, String>, meaning the keys are of type Integer and the values are of type String.

5. Putting Value pairs:

hm.put(100, “Rajendra”);

hm.put(102, “Praveen”);

hm.put(101, “Bipin”);

hm.put(103, “Pankaj”);

The put() method is used to add key-value pairs to the Hashtable:

  • Key 100 is mapped to the value  “Rajendra”.
  • Key 102 is mapped to the value  “Praveen”.
  • Key 101 is linked with the value  “Bipin”.
  • Key 103 is linked with the value  “Pankaj”.

6. Passing Through Hashtable:

for (Map.Entry m : hm.entrySet()) {

System.out.println(m.getKey() + ” ” + m.getValue());

  }

  • hm.entrySet(): The method return a Set of entries from the Hashtable, where every entry is an object of type Map.Entry.

for (Map.Entry m : hm.entrySet()): This loop iterates through each entry in the set. For each entry m, it retrieves the key and the value:

  • m.getKey() retrieves the key.
  • m.getValue() retrieves the value.

The System.out.println(m.getKey() + ” ” + m.getValue()) prints each key-value pair on a new line.

7. Output:

The output will print the key-value pairs of the Hashtable:

The order of the key-value pairs is not guaranteed since Hashtable doesn’t keep a specific order of its elements. It’s based on a hash function, and the order is random.

Possible Output:

100 Rajendra

101 Bipin

102 Praveen

103 Pankaj

The actual order may differ based on how the Hashtable is internally structured and the hash values of the keys.

Key Concepts:

1. Hashtable:

Hashtable is a class of the package java.util. It implements the interface Map. It contains the mapping of the key-value pair. The keys or values can not be null. It is synchronized; it’s safe from thread issues, though its performance is somewhat hit because of this feature.

  It has no defined order for its entries (its ordering depends upon the hash codes of its keys).

2. put() Method:

This is used to add a key-value pair to the Hashtable. If the key already exists in the map, then it simply updates the value associated with the key.

3. entrySet() Method:

It is used to retrieve a Set view of the mappings that are contained in the map. This comes in very handy when one needs to access all entries within the map.

4. Map.Entry:

Map.Entry is a nested interface of the Map interface, representing a key-value pair. It provides methods like getKey() and getValue() to access the key and value of the entry.

5. Looping through collection using for-each loop:

The enhanced for loop to iterate over the collection of entries. You can access the key using m.getKey() and the value using m.getValue() for each Map.Entry.

Time Complexity:

  • Insertion (put()) : Average O(1) time complexity but could be O(n) for worst-case in terms of hash collisions as Hashtable depends on a hashing mechanism to put the entries.
  • Iteration (entrySet() and for-each loop): O(n), where n is the number of entries in the map. It is a linear time operation to iterate through the entries of the map.

Conclusion

  • This program shows how a Hashtable can be used to store and retrieve key-value pairs. 
  • This program shows how entries are added to the Hashtable and how they are iterated using the entrySet() method. 
  • The output will contain key-value pairs that are stored in the Hashtable, but it is not possible to predict which order they might appear in, because the hash mechanism used by the Hashtable internally controls the order of the pairs.

Leave a Comment

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