-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMapPart1.java
51 lines (45 loc) · 1.5 KB
/
HashMapPart1.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
41
42
43
44
45
46
47
48
49
50
51
package hashmap.part1;
import java.util.HashMap;
import java.util.Scanner;
//advantages of hashmap
/*
1.If no synchronization is needed
2.fast to insert ,search or delete
3.Updated version of hashtable
4.Stores values against key and support only one null key and multiple null values
*/
public class HashMapPart1 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
HashMap<String,Long> map=new HashMap<>();
print(map);
System.out.println("Enter size of phonebook:");
int size=scan.nextInt();
for(int i=0;i<size;i++){
System.out.println("Key:");
String key=scan.next();
System.out.println("Value:");
long value=scan.nextLong();
map.put(key,value);
}
print(map);
System.out.println("Enter key to get value:");
String getKey=scan.next();
if(map.containsKey(getKey)){
long getValue=map.get(getKey);
System.out.println("The value for key : "+getKey+" is : "+getValue);
}
else{
System.out.println("No results found.");
}
System.out.println("Size of the map or phonebook:"+map.size());
}
public static void print(HashMap<String,Long> map){
if(map.isEmpty()){
System.out.println("PhoneBook is empty.");
}
else{
System.out.println(map);
}
}
}