source code Hashtable
package myutil;
// HASHTABLE
import java.io.*;
import java.util.*;
public class HashtableInet
{
public static void main(String args[])throws IOException
{
//Create an empty hash table
Hashtable ht=new Hashtable();
//Store keyvalue pairs into ht
ht.put("sachin", new Integer(89));
ht.put("dravid", new Integer(87));
//Retreive the keys from list
Enumeration e=ht.keys();
//display the keys from e
System.out.println("Player names :");
while(e.hasMoreElements())
System.out.println(e.nextElement());
//accepting playername from keyboard
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter playername");
String name=br.readLine();
//Retreive score of player
Integer score=(Integer)ht.get(name);
if(score!=null)
{
int sc=score.intValue();
System.out.println(name + "scored runs :" + sc);
}
else
System.out.println("player not found");
}
}
output:
Player names :
dravid
sachin
Enter playername
dravid
dravidscored runs :87
0 comments:
Post a Comment