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 number = ""; Map yellowPages = new TreeMap(); public PhoneBook(){ } public void add(String name, String phoneNumber){ yellowPages.put(name, phoneNumber); } public void remove(String name) { yellowPages.remove(name); } public String lookUp(String name) { String number = yellowPages.get(name); return number; } public int getSize(){ return yellowPages.size(); } public String reverseLookUp(String value) { String key = ""; for(Map.Entry entry : yellowPages.entrySet()) if(value.equals(entry.getValue())){ key = entry.getKey().toString(); } return key; } public String display() { Set set = yellowPages.entrySet(); Iterator it = set.iterator(); StringBuilder answerBuild = new StringBuilder(); while(it.hasNext()) { Map.Entry x = (Map.Entry)it.next(); answerBuild.append(x.getKey() + " : " + x.getValue()); } String answer = answerBuild.toString(); return answer; } }