Thursday, March 5, 2009

example code HashMap



package myutil;

//HASHMAP

import java.io.*;
import java.util.*;
public class HashMapInet
{
public static void main(String args[])throws IOException
{
HashMap hm=new HashMap();
String name,str;
Long phno;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//MENU
while(true)
{
System.out.println("1.Enter entries into phone book");
System.out.println("2.Look up in phone book");
System.out.println("Enter ur choice");
int n=Integer.parseInt(br.readLine());
switch(n)
{
case 1:
System.out.println("Enter name");
name=br.readLine();
System.out.println("Enter phone no");
str=br.readLine();
phno=new Long(str);
hm.put(name,phno);
break;
case 2:
System.out.println("Enter name");
name=br.readLine();
phno=(Long)hm.get(name);
System.out.println("phone no=" + phno);
break;
default:
return;
}
}
}
}

output:
1.Enter entries into phone book
2.Look up in phone book
Enter ur choice
1
Enter name
Laxman
Enter phone no
9885988311
1.Enter entries into phone book
2.Look up in phone book
Enter ur choice
2
Enter name
Laxman
phone no=9885988311
1.Enter entries into phone book
2.Look up in phone book
Enter ur choice





1 comments:

Anonymous said...

how to choose a student at random every week using map collection interface in java

Post a Comment