/** * Created by leon on 1/23/18. */ import java.util.*; public class PhoneBook { private TreeMap> book = new TreeMap>(); public ArrayList add(String name, String phoneNumber) { ArrayList converted = new ArrayList(); if (book.containsKey(name)) converted = book.get(name); converted.add(phoneNumber); add(name, converted); return book.get(name); } public ArrayList add(String name, ArrayList phoneNumbers) { book.put(name, phoneNumbers); return book.get(name); } public TreeMap> remove(String name) { book.remove(name); return book; } public ArrayList remove(String name, String number) { ArrayList numbers = book.get(name); numbers.remove(numbers.indexOf(number)); book.put(name, numbers); return book.get(name); } public ArrayList lookup(String name) { return book.get(name); } public String reverseLookup(String phoneNumber) { String result = "none"; for(Map.Entry> entry : book.entrySet()) { ArrayList numbers = entry.getValue(); for (String number : numbers) { if (number.equals(phoneNumber)) result = entry.getKey(); } } return result; } public String display() { StringBuilder result = new StringBuilder(); for(Map.Entry> entry : book.entrySet()) { result.append(entry.getKey() + " "); ArrayList numbers = entry.getValue(); for (String number : numbers) { result.append(number + " "); } result.append("\n"); } return result.toString(); } public static void main(String[] args) { PhoneBook phoneBook = new PhoneBook(); }; }