how to get the keys and values contained in a Hashtable via enumeration using collections in java
Previous | Home | Next |
In this example we will fetch the keys and the values of a Hashtable using the enumeration.
For creating a Hashtable first of all we have to import the java.util package in which the java.util.Hashtable class is defined. In this example we have created a class named CollectionExample in which we have created a Hashtable and added some elements to it.
In this example we have used the enumeration for storing the keys and values of the Hashtable.
In this example we have created a Hashtable and added the elements by using the put(Object key, Object value) method on its object i.e ht. Then we have created an enumeration, which contains the values of the Hashtable, named enu and get those values by using the following statments,while(enu.hasMoreElements())
{
System.out.println(enu.nextElement());
}
In the same manner we have created an enumeration named enu2 for getting the keys of the Hashtable.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String[] args) throws NullPointerException
{
// TODO Auto-generated method stub
try
{
Hashtable<Integer,String> ht=new Hashtable<Integer,String>(); // created a Hashtable named ht in which key is numeric and value is String types
ht.put(1, "r4r"); // adding the values to the hashtable using key-value pairs
ht.put(2, "development");
ht.put(3, "tutorials");
ht.put(4, "shashi");
ht.put(5, "software developer");
System.out.println("The size of the Hashtable ht is: "+ht.size()); // displaying the size of the Hashtable
System.out.println("The elements according to key are: ");
for(int i=1;i<=ht.size();i++)
{
System.out.println("key = "+i+","+"Value = "+ ht.get(i)); // displaying the key-value pair contained in the Hashtable
}
Enumeration<String> enu=ht.elements(); // created an enumeration enu for holding the elements(String types) of Hashtable
System.out.println("The elements of the Hashtable displayed via enumeration are: ");
while(enu.hasMoreElements())
{
System.out.println(enu.nextElement()); // displaying the elements of the Hashtable contained in the enu
}
// System.out.println(ht.values());
Enumeration<Integer> enu2=ht.keys(); // created an enumeration enu2 for holding the key(Integer types) of Hashtable
System.out.println("The keys of the Hashtable displayed via enumeration are: ");
while(enu2.hasMoreElements())
{
System.out.println(enu2.nextElement()); // displaying the keys of the Hashtable contained in enu2
}
}
catch(NullPointerException e) // handling the NullPointer Exception which is thrown
{
e.printStackTrace();
}
}
}
The output of the above given example is as follows:
The size of the Hashtable ht is: 5
The elements according to key are:
key = 1,Value = r4r
key = 2,Value = development
key = 3,Value = tutorials
key = 4,Value = shashi
key = 5,Value = software developer
The elements of the Hashtable displayed via enumeration are:
software developer
shashi
tutorials
development
r4r
The keys of the Hashtable displayed via enumeration are:
5
4
3
2
1
Previous | Home | Next |