Java Java Data Structures Efficiency! Using a Map to store Contact Methods

Jiten Mistry
Jiten Mistry
4,698 Points

Im not sure how to complete this exercise, could someone please help

Im a bit stuck on the logic behind this, could someone please give me pointers on how to complete it please.

com/example/model/Contact.java
package com.example.model;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;

public class Contact {
  private String mFirstName;
  private String mLastName;
  private Map<String, String> mContactMethods;

  public Contact(String firstName, String lastName) {
    mFirstName = firstName;
    mLastName = lastName;
    /* This stores contact methods by name
     * eg:  "phone" => "(555) 555-1234"
     */
    mContactMethods = new HashMap<String, String>();
  }

  public void addContactMethod(String method, String value) {
    // TODO: Add to the contact method map
      mContactMethods.put(method, value);
  }

  /**
   * Returns the available contact methods.  eg: phone, pager,
   *
   * @return The name of the contact methods that are available
   */
  public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    Set<String> contacts = new HashSet<String>();
    mContactsMethod.add(

    return contacts;
  }

  /**
   * Returns the value for the contact method if it exists, 
   *
   * @param methodName  The name of the contact method to look up.
   * @return The name of the contact methods that are available
   */
  public String getContactInfo(String methodName) {
    // FIXME: return the value for the passed in *methodName*
    return null;
  }

  public String getFirstName() {
    return mFirstName;
  }

  public String getLastName() {
    return mLastName;
  }

}

3 Answers

Jeremy Faith
PRO
Jeremy Faith
Pro Student 56,688 Points

It looks like you are stuck on Task 2. Following the hint How about a set of keys from mContactMethods?, you can use the method keySet() to access the keys from a map. Just return mContactMethods.keySet().

return mContactMethods.keySet()

Hope this helps.

Chris Howell
Chris Howell
Treehouse Staff

Ahh darn, apparently I need my morning coffee. Good attention to detail.

Refer to this answer instead of mine. Mine refers to Step 1 and may mislead you.

I read the question and somehow missed step 1 was finished. Ugh.

Chris Howell
STAFF
Chris Howell
Treehouse Staff

Can you be more descriptive about which part you aren't understanding?

Step 1 says: In this first step, modify the addContactMethod method to store the information in the mContactMethods Map.

So youll want to scroll down to

public void addContactMethod(String method, String value){
    // TODO: Add to the contact method map
}

just by looking at the method you can tell by void that nothing is going to be returned from it. It will only do something inside of it when it is called.

Now I dont want to just give you the answer here, because in coding, breaking your code is how you learn. A good way to figure out what you need to do is think about what the code looks like that would call this Contact object. So I will show you what that might look like to see if you follow and if that helps. Okay? Dont copy this because this wont pass this objective. Its an example to show you whats going to happen. Ill use comments in the code.

/** creating a new Contact object stored in newContact
** Property: mFirstName = "Mike"
** Property: mLastName = "TheFrog"
** Property: mContactMethods  is an empty Map here.
**/
Contact newContact = new Contact("Mike", "TheFrog");

/**
 ** Here I want to call the addContactMethod and be able to pass in TWO strings that get added to my Map called mContactMethods by the logic YOU add to do so. Another contact method I could of used instead could of been "email", "mikethefrog@example.com". 
**/
newContact.addContactMethod("phone", "(555) 555-1234");

does this kind of help? let me know

shu Chan
shu Chan
2,951 Points

Thank you Chris! your answer inadvertently answered my question!