1

I was trying to do an exercise creating a PhoneBook using HashMap.
However I see my addPhone method doesn't add a new phone to my PhoneBook pb i.e. data.put(name, num); method inside my addPhone doesn't put the data into the HashMap data.

Can somebody explain me what is wrong here?

UPD Now I understand it was a mistake, I used containsValue method instead of containsKey. So simple!
But this question is not similar at all to the suggested already existing question. I was not asking Is checking for key existence in HashMap always necessary? I know about the ways to search the HashMap according to the key or to the value. This question is actually caused by a mistake. However I received a very wide and useful answers here. I believe these answers, especially davidxxx's answer is excellent and may be useful for many people.

import java.util.HashMap;


public class PhoneBook {

        private HashMap<String, String> data;

        public PhoneBook() 
        { 
            data = new HashMap<String, String>(); 
        } 
        public void addPhone(String name, String num) 
        { 
            data.put(name, num); 
        }


        //a
        public String getPhone(String name){
            if(data.containsValue(name)){
                return data.get(name);
            }
            else 
                return null;
        }

        //b
        public void ToString(){
            data.toString();
        }

        public static void main(String[] args) {

            PhoneBook pb = new PhoneBook();
            pb.addPhone("shlomi", "12312413yuioyuio24");
            pb.addPhone("shlomi1", "1231345345241324");
            pb.addPhone("shlomi2", "12312445735671324");
            System.out.println(pb.getPhone("shlomi"));
            System.out.println(pb.getPhone("blat"));
            pb.ToString();
    }
}
Prophet
  • 32,350
  • 22
  • 54
  • 79

3 Answers3

8

You provide the name that is the key to data.containsValue(name) instead of the value.
What you need is Map.containskey() if you want to return the value according to the key from the client side of your class.

Note that handling the existence in the map is not required as null is returned as no mapping exists for a key :

public String getPhone(String name){       
     return data.get(name);      
}

Side note

Not the issue in the question but whatever an issue to handle.
ToString() is really not a good name for a method :

public void ToString(){
    data.toString();
}

Method names are case sensitive, yes, but it is not a fair reason to play with that to define a slightly different naming (here is the T uppercase) to the Object.toString() method. It makes the code reading misleading.
Besides, your method returns nothing. So this is helpless : pb.ToString();

What you should declare is :

@Override
public String toString(){
    return data.toString();
}

Adding @Override adds a compilation constraint that checks that the method is defined in the hierarchy.

Now you can for example write in the standard output the toString() representation of your PhoneBook object in this way :

System.out.println(pb);
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • @Eliyahu You are welcome :) I added an additional example to use the `toString()` from the client side as invoking only `pb.ToString()` is still useless if you don't exploit the returned String. – davidxxx Mar 16 '18 at 14:50
2

HashMap maps keys to values so it contains key-value pairs.

containsValue() it returns true if map maps one or more keys to the specified value

containsKey() It returns true if map contains a mapping for the specified key

Your case has name as key and num as value. In the method getPhone() you have parameter which corresponds to name and hence you should use containsKey() instead of containsValue()

nits.kk
  • 5,204
  • 4
  • 33
  • 55
1

data.containsValue(name) checks whether the HashMap contains the value name. Since your HashMap contains name keys and number values, you should be calling data.containsKey(name).

public String getPhone(String name){
    if(data.containsKey(name)) {
        return data.get(name);
    } else 
        return null;
}

or simply

public String getPhone(String name) {
    return data.get(name);
}
Eran
  • 387,369
  • 54
  • 702
  • 768