12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
-
- import java.util.*;
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- private Map<String, ArrayList<String>> phoneBook;
-
- public PhoneBook() {
- phoneBook = new TreeMap<String, ArrayList<String>>();
- }
-
- public ArrayList<String> add(String name, String number) {
- if (phoneBook.get(name) == null) {
- phoneBook.put(name, new ArrayList<String>());
- }
- phoneBook.get(name).add(number);
-
- return phoneBook.get(name);
- }
-
- public ArrayList<String> lookup(String name) {
- return phoneBook.get(name);
- }
-
- public String reverseLookup(String phoneNumber) {
- String name = "not found";
- for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
- for (String numbers : entry.getValue()) {
- if (numbers.equals(phoneNumber)) name = entry.getKey();
- }
- }
- return name;
- }
-
- public String display() {
- StringBuilder phoneNumbers = new StringBuilder();
- for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
- phoneNumbers.append(entry.getKey() + " ");
- for (String number : entry.getValue()) {
- phoneNumbers.append(number + " ");
- }
- phoneNumbers.append("\n");
- }
- return phoneNumbers.toString();
- }
-
- }
|