Thursday, March 5, 2009

Example code Hashtable



package myutil;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashtableEx1 {
public static void main(String[] args) {
Hashtable table=new Hashtable();
table.put(new Integer(1),"xxx");
table.put(new Integer(24),"yyy");
table.put(new Integer(3),"zzz");
table.put(new Integer(8),"aaa");
table.put(new Integer(9),"bbb");
table.put(new Integer(2),"sss");
System.out.println("The Hashtable is "+table);
System.out.println("using map entry methods");
Set s=table.entrySet();
Iterator i= s.iterator();
while(i.hasNext())
{
Map.Entry me=(Map.Entry)i.next();
System.out.println(me.getKey()+"....."+me.getValue());

}
}
}

output:
The Hashtable is {9=bbb, 8=aaa, 3=zzz, 2=sss, 24=yyy, 1=xxx}
using map entry methods
9.....bbb
8.....aaa
3.....zzz
2.....sss
24.....yyy
1.....xxx



0 comments:

Post a Comment