Dictionary keys() in Java

keys(): This method is available in java.util.Dictionary class of Java.

Syntax:

Enumeration<K> java.util.Dictionary.keys()

This method returns an enumeration of the keys in this dictionary.

Parameters: NA

Returns: an enumeration of the keys in this dictionary.

Exceptions: NA

Approach

Java

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

public class Dictionarykeys {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        Enumeration<String> enumeration = dictionary.keys();

        while (enumeration.hasMoreElements()) {
            System.out.print(enumeration.nextElement() + " ");

        }

    }
}

Output:

Java C++ Hello 

No comments:

Post a Comment