Java Dictionary - keys() Method
The java.util.Dictionary.keys() method returns an enumeration of the keys in this dictionary. The returned Enumeration will generate all the keys contained in entries in this dictionary.
Syntax
public abstract Enumeration<K> keys()
Here, K is the type of key maintained by the container.
Parameters
No parameter is required.
Return Value
Returns an enumeration of the keys in this dictionary.
Exception
NA.
Example:
In the example below, the java.util.Dictionary.keys() method returns an enumeration of the keys in the given dictionary.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a dictionary Dictionary<Integer, String> Dict = new Hashtable<Integer, String>(); //populating dictionary Dict.put(101, "John"); Dict.put(102, "Marry"); Dict.put(103, "Kim"); Dict.put(104, "Sam"); Dict.put(105, "Jo"); //printing the content of the dictionary System.out.println("Dict contains: " + Dict); //creating an Enum of keys of the dictionary Enumeration MyEnum = Dict.keys(); //printing the Enum info System.out.println("MyEnum is: " + MyEnum); //printing the content of the Enum System.out.print("MyEnum contains: "); while (MyEnum.hasMoreElements()) { System.out.print(MyEnum.nextElement() + " "); } } }
The output of the above code will be:
Dict contains: {105=Jo, 104=Sam, 103=Kim, 102=Marry, 101=John} MyEnum is: java.util.Hashtable$Enumerator@2f2c9b19 MyEnum contains: 105 104 103 102 101
❮ Java.util - Dictionary