1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
-
- import java.util.TreeMap;
- import java.util.*;
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- TreeMap<String, ArrayList<String>> pb;
-
- public PhoneBook(){
- pb = new TreeMap<>();
-
- }
-
- public void addElement(String name, String phoneNumber){
- ArrayList<String> aList = pb.containsKey(name) ? pb.get(name): new ArrayList<String>();
- aList.add(phoneNumber + "\n");
- pb.put(name, aList);
- }
-
- public void remove(String name){
- pb.remove(name);
- }
-
- public String lookUp(String name){
- StringBuilder look = new StringBuilder();
-
- for(String element: this.pb.get(name)){
-
- look.append(element);
-
-
- }
- return look.toString();
- }
-
- public String reverseLookup(String phoneNumber){
- String gettingThatValue = null;
- for(Map.Entry<String,ArrayList<String>> element: pb.entrySet()){
- if(element.getValue().contains(phoneNumber)){
- gettingThatValue = element.getKey();
- break;
- }
- }
-
- return gettingThatValue;
- }
-
- public void display(){
- for(Map.Entry<String,ArrayList<String>> element:pb.entrySet()){
- System.out.print(element.getKey() + ":" + element.getValue().toString());
- }
- }
-
- }
|