import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; /** * Created by leon on 1/23/18. */ public class PhoneBook { String name; String phoneNumber = ""; Map phoneBook = new TreeMap(); public PhoneBook() { } public void add(String name, String phoneNumber) { phoneBook.put(name, phoneNumber); } public void remove(String name) { phoneBook.remove(name); } public String lookup(String name) { String phoneNumber = phoneBook.get(name); return phoneNumber; } public String reverseLookup(String value){ String key = ""; for(Map.Entry entry : phoneBook.entrySet()) if (value.equals(entry.getValue())) { key = entry.getKey().toString(); } return key; } public String display() { Set set = phoneBook.entrySet(); Iterator iter = set.iterator(); StringBuilder answer = new StringBuilder(); while(iter.hasNext()) { Map.Entry i = (Map.Entry)iter.next(); answer.append(i.getKey() + " : " + i.getValue()); } String result = answer.toString(); return result; } public int getSize() { return phoneBook.size(); } }