It seems to me that what you really want is to make the name to be key of the map and the corresponding telephone number set the value.
Also you want that your keys are not case sensitive... String instances that differ in case will have different hash-codes and so you cannot use them here as the key per se. What you need to do then is to transform names into a canonical form, say all lowercase, when accessing the map so that difference in case is not relevant any longer.
There is a few way to go about this... extending a HashMap to suit your needs is an elegant one.
Better to use a String to store phone number as often they do contain non numeric characters...
public class PhoneBook extends HashMap<String,Set<String>> {
public PhoneBook() { }
public PhoneBook(int initialCapacity) { super(initialCapacity); }
// Use this method to add numbers to the phone-book
// returns true if the phone directory changed as a result of the call.
public boolean add(String name, String number) {
String canonicalName = name.toLowerCase();
Set<String> existingNumbers = super.get(name);
if (existingNumbers == null)
super.put(canonicalName,existingNumbers = new HashSet<>(10));
// give an estimate capacity per name, in this example 10.
return existingNumbers.add(number);
}
@Override
public Set<String> put(String name, Set<String> numberSet) {
throw new UnsupportedOperationException("you must use add(String) to add numbers");
}
@Override
public Set<String> get(String name) {
String canonicalName = name.toLowerCase();
Set<String> existingNumbers = super.get(canonicalName);
return existingNumbers == null ? Collections.EMPTY_SET : existingNumbers;
}
}
You may need to override some other operations from Map/Hash map to make sure consistency is preserved.