-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMappingPhonebookHashMap.java
40 lines (33 loc) · 1.18 KB
/
MappingPhonebookHashMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package mapping.phonebook.hashmap;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class MappingPhonebookHashMap {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String,Integer> phonebook=new HashMap<String,Integer>();
System.out.println("Enter phonebook length:");
int n = in.nextInt();
for(int i = 0; i < n; i++){
System.out.println("Enter username:");
String name = in.next();
System.out.println("Enter user phone no:");
int phone = in.nextInt();
// Wentering the value into the map
phonebook.put(name,phone);
}
while(in.hasNext()){
System.out.println("Enter the key to search:");
String s = in.next();
if(phonebook.containsKey(s)){
//getting the value by means of the key
int value=phonebook.get(s);
System.out.println(s+"="+value);
}
else{
System.out.println("Not found");
}
}
in.close();
}
}