Hashtable keys() Method in Java (original) (raw)

Last Updated : 9 Oct, 2021

As we all know enumeration defines java class type so do enumerations can have constructors, methods, and instance variables. The java.util.Hashtable.keys() method of Hashtable class in Java is used to get the enumeration of the keys present in the hashtable.

Illustration:

Hashtable keys() Method in Java

Syntax:

public Enumeration keys() Enumeration enu = Hash_table.keys();

Return value: An enumeration of the keys of the Hashtable.

Example 1:

Java `

// Java Program to Illustrate keys() method // of Hashtable class

// Importing utility classes import java.util.*;

// Main class public class GFG {

// Main driver method
public static void main(String[] args)
{
    // Creating an empty Hashtable
    // Declaring object of integer and string type
    Hashtable<Integer, String> hash_table
        = new Hashtable<Integer, String>();

    // Inserting elements into the table
    // using put() method

    // Custom input elements
    hash_table.put(10, "Geeks");
    hash_table.put(15, "4");
    hash_table.put(20, "Geeks");
    hash_table.put(25, "Welcomes");
    hash_table.put(30, "You");

    // Print and display all entered elements in object
    System.out.println("The Table is: " + hash_table);

    // Creating an empty enumeration to store
    Enumeration enu = hash_table.keys();

    // Display message
    System.out.println("The enumeration of keys are:");

    // Condition holds true till there is single element
    // remaining using hasMoreElements() method
    while (enu.hasMoreElements()) {

        // Displaying the Enumeration
        System.out.println(enu.nextElement());
    }
}

}

`

Output:

The Table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes} The enumeration of keys are: 10 20 30 15 25

Example 2:

Java `

// Java Program to illustrate keys() method // of Hashtable class

// Importing utility classes import java.util.*;

// Main class public class GFG {

// Main driver method
public static void main(String[] args)
{
    // Creating an empty Hashtable
    // Declaring object of string and integer type
    Hashtable<String, Integer> hash_table
        = new Hashtable<String, Integer>();

    // Inserting elements into the table
    // using put() method
    hash_table.put("Geeks", 10);
    hash_table.put("4", 15);
    hash_table.put("Geeks", 20);
    hash_table.put("Welcomes", 25);
    hash_table.put("You", 30);

    // Displaying the Hashtable
    System.out.println("The Table is: " + hash_table);

    // Creating an empty enumeration to store
    Enumeration enu = hash_table.keys();

    // Display message
    System.out.println("The enumeration of keys are:");

    // Condition holds true till there is single element
    // remaining via hasMoreElements() method
    while (enu.hasMoreElements()) {

        // Displaying the enumeration
        System.out.println(enu.nextElement());
    }
}

}

`

Output:

The Table is: {You=30, Welcomes=25, 4=15, Geeks=20} The enumeration of keys are: You Welcomes 4 Geeks