Java Hashtable elements() Method Example

The elements() method of java.util.Hashtable class returns an enumeration of the values in this hashtable. Use the Enumeration methods on the returned object to fetch the elements sequentially.

Signature

public Enumeration<V> elements()

This method returns an enumeration of the values in this hashtable. Use the Enumeration methods on the returned object to fetch the elements sequentially.

Example

The following example shows the usage of java.util.Hashtable.elements() method.

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

public class ElementsMethodHashtableExample 
{
    public static void main(String args[])
    {
        // create an empty Hashtable
        Hashtable<Integer,String>  hashtable1 = new Hashtable<Integer,String>();
        
        // use put() method to put elements to the Hashtable
        hashtable1.put(1,"Element1");
        hashtable1.put(2,"Element2");
        hashtable1.put(3,"Element3");
        hashtable1.put(4,"Element4");
        hashtable1.put(5,"Element5");
        
        //Get the elements of hastable1
        Enumeration enumeration  = hashtable1.elements();
        //Print the elements of the hashtable1
        System.out.println("**Elements of the hastable1**");
        while(enumeration.hasMoreElements())
        {
            System.out.println(enumeration.nextElement());
        }
    }
}

Output

**Elements of the hastable1**
Element5
Element4
Element3
Element2
Element1

Leave a Reply

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