Friday, September 16, 2011

HashTable implemented by List


//Print a HashTable's key and values where key is an Integer element and value is a ArrayList.
//HashTableImplementedByList.java
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

public class HashTableImplementedByList
{
public static void main(String arg[])
{
List list = new ArrayList();
for (int i = 1; i < 11; i++)
{
list.add(i);
}
Hashtable> hashtable = new Hashtable>();

hashtable.put(1, list);

Enumeration e;

System.out.print("HashTable key is: ");
e = hashtable.keys();
while (e.hasMoreElements())
{
Object o = (Object) e.nextElement();
System.out.println(o);
}
System.out.print("HashTable elements are : ");
e = hashtable.elements();
while (e.hasMoreElements())
{
Object o = (Object) e.nextElement();
System.out.println(o);

}
}
}


Output =>
HashTable key is: 1
HashTable elements are : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

No comments:

Post a Comment