📜  Java中的哈希表键()方法

📅  最后修改于: 2022-05-13 01:55:13.518000             🧑  作者: Mango

Java中的哈希表键()方法

众所周知,枚举定义了Java类类型,所以枚举可以有构造函数、方法和实例变量。 Java Hashtable 类的Java .util.Hashtable.keys() 方法用于获取哈希表中存在的键的枚举。

插图:

Java中的哈希表键()方法

句法:

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

返回值: Hashtable 的键的枚举。

示例 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 hash_table
            = new Hashtable();
 
        // 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());
        }
    }
}


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 hash_table
            = new Hashtable();
 
        // 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());
        }
    }
}


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

示例 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 hash_table
            = new Hashtable();
 
        // 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());
        }
    }
}
输出:
The Table is: {You=30, Welcomes=25, 4=15, Geeks=20}
The enumeration of keys are:
You
Welcomes
4
Geeks