1234567891011121314151617181920212223242526272829303132333435363738394041 |
-
- import java.util.*;
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- //private String name;
- //private String phoneNum;
- TreeMap<String, String> phoneListByName = new TreeMap();
- TreeMap<String, String> phoneListByNum = new TreeMap();
-
- public void add(String name, String phoneNumber){
- phoneListByName.put(name, phoneNumber);
- phoneListByNum.put(phoneNumber, name);
-
- }
- public void remove(String name){
- String phoneNumber = lookUp(name);
- phoneListByName.remove(name);
- phoneListByNum.remove(phoneNumber);
- }
- public String lookUp(String name){
- return phoneListByName.get(name);
-
- }
- public String reverseLookUp(String phoneNumber){
- return phoneListByNum.get(phoneNumber);
-
- }
- public void display(){
- Set set = phoneListByName.entrySet();
- //Set set2 = phoneListByNum.entrySet();
- Iterator i = set.iterator();
-
- while(i.hasNext()){
- Map.Entry name = (Map.Entry)i.next();
- System.out.println(name.getKey() + ": " + name.getValue()) ;
- }
- }
- }
|