This repository has been archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathHashMap.java
125 lines (99 loc) · 4.91 KB
/
HashMap.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.HashMap;
import java.util.Scanner;
class HashMapDemo {
//constant command strings
private static final String COMMAND_ADD= "a";
private static final String COMMAND_REMOVE = "r";
private static final String COMMAND_VALUE = "v";
private static final String COMMAND_IS_KEY_EXISTS = "?k";
private static final String COMMAND_IS_VALUE_EXISTS = "?v";
private static final String COMMAND_ALL_KEY = "ak";
private static final String COMMAND_ALL_VALUE = "av";
private static final String COMMAND_SIZE = "si";
private static final String COMMAND_SHOW = "sh";
private static final String COMMAND_QUIT= "q";
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
// HashMap is a key-value pair data structure. Here, Key ( String ) - value (String ) type
HashMap<String, String > hashMap = new HashMap<>();
System.out.println("Instructions:");
System.out.println("\t1. Add value: \""+COMMAND_ADD+" key value\" . Example: \"a name Rezwan\" here, key = 'name' , value = 'Rezwan' ");
System.out.println("\t2. Show value: \""+COMMAND_VALUE+" key_name_here\"");
System.out.println("\t3. Remove value: \""+COMMAND_REMOVE+" key_name_here\"");
System.out.println("\t4. check if key exists : \""+COMMAND_IS_KEY_EXISTS+" key_name_here\"");
System.out.println("\t5. check if value exists : \""+COMMAND_IS_VALUE_EXISTS+" value_name_here\"");
System.out.println("\t7. See ALl Keys : \""+COMMAND_ALL_KEY+"\"");
System.out.println("\t8. See ALl Values : \""+COMMAND_ALL_VALUE+"\"");
System.out.println("\t9. See current HashMap conditions: \""+COMMAND_SHOW+"\"");
System.out.println("\t10. Size of the HashMap: \""+COMMAND_SIZE+"\"");
System.out.println("\t11. Exit program: \""+COMMAND_QUIT+"\"");
while (true) {
// dividing the command string and number value
String[] tempCommand = myScanner.nextLine().split(" ");
try{
String commandName = String.valueOf(tempCommand[0]);
String str1 = "", str2 = "";
if(tempCommand.length == 2){
str1 = String.valueOf(tempCommand[1]);
}
else if(tempCommand.length == 3){
str1 = String.valueOf(tempCommand[1]);
str2 = String.valueOf(tempCommand[2]);
}
//checking command name and performing accordingly
switch (commandName){
case COMMAND_ADD:
hashMap.put(str1, str2);
System.out.println(str1+ "-" +str2 + " added");
break;
case COMMAND_VALUE:
System.out.println( hashMap.get(str1) + " is the value of KEY = "+ str1);
break;
case COMMAND_REMOVE:
hashMap.remove(str1);
System.out.println( "Pair of " +str1 + " KEY is removed");
break;
case COMMAND_IS_KEY_EXISTS:
if(hashMap.containsKey(str1)){
System.out.println(str1 + " key exists in the hashmap");
}
else{
System.out.println(str1 + " key doesn't exist in the hashmap");
}
break;
case COMMAND_IS_VALUE_EXISTS:
if(hashMap.containsValue(str1)){
System.out.println(str1 + " value exists in the hashmap");
}
else{
System.out.println(str1 + " value doesn't exist in the hashmap");
}
break;
case COMMAND_ALL_KEY:
System.out.println("All Keys: "+hashMap.keySet());
break;
case COMMAND_ALL_VALUE:
System.out.println("All Values: "+hashMap.values());
break;
case COMMAND_SIZE:
System.out.println("HashMap size is "+hashMap.size()+" right now");
break;
case COMMAND_SHOW:
System.out.println(hashMap);
break;
case COMMAND_QUIT:
return;
default:
System.out.println("Invalid command format");
break;
}
}
catch (NumberFormatException e){
System.out.println("Invalid command format");
}
catch (Exception e){
System.out.println("Exception: "+ e.getMessage());
}
}
}
}