|
@@ -1,7 +1,31 @@
|
1
|
|
-
|
|
1
|
+import java.util.*;
|
|
2
|
+import java.util.Map.Entry;
|
2
|
3
|
|
3
|
|
-/**
|
4
|
|
- * Created by leon on 1/23/18.
|
5
|
|
- */
|
6
|
|
-public class PhoneBook {
|
7
|
|
-}
|
|
4
|
+
|
|
5
|
+public class PhoneBook {
|
|
6
|
+
|
|
7
|
+ Map<String, List<String>> phoneMap = new TreeMap<String, List<String>>();
|
|
8
|
+ List<String> phoneNumbers = new ArrayList<String>();
|
|
9
|
+
|
|
10
|
+ public void add(String name, String phoneNumber){
|
|
11
|
+ phoneNumbers.add(phoneNumber);
|
|
12
|
+ phoneMap.put(name, phoneNumbers);
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+ public void remove(String name){
|
|
16
|
+ phoneMap.remove(name);
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ public String lookup(String name){
|
|
20
|
+ return phoneNumbers.iterator(phoneMap.get(name));
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ public String reverseLookup(String phoneNumber){
|
|
24
|
+ for(Entry<String, String> entry : phoneMap.entrySet()){
|
|
25
|
+ if(entry.getValue().equals(phoneNumber)){
|
|
26
|
+ return entry.getKey();
|
|
27
|
+ }
|
|
28
|
+ }
|
|
29
|
+ return null;
|
|
30
|
+ }
|
|
31
|
+}
|