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

How do I fix the getAvailableContacts method?

Hi there,

I'm working on challenge task 2 of 3, and I'm trying to get through this code in the getAvailableContacts method. Do you have any suggestions?

Thanks!

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

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

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

  public Contact(String firstName, String lastName) {
    mFirstName = firstName;
    mLastName = lastName;
    mContactMethods = new HashMap<String, String>();
  }

  public void addContactMethod(String method, String value) {
    mContactMethods.put(method, value);

  }

  public Set<String> getAvailableContactMethods() {
    private Map<String, String> mContactMethods;
  }

  public String getContactInfo(String methodName) {
    return null;
  }

  public String getFirstName() {
    return mFirstName;
  }

  public String getLastName() {
    return mLastName;
  }

}

Refer to the below code for help. The answers to your questions are in the video itself. :) I would say go through the video again whenever you get stuck on a challenge and you will find your answer. :-)

package com.example.model;

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

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> allMethods = mContactMethods.keySet();
    return allMethods;
  }

  /**
   * 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 mContactMethods.get(methodName);
  }

  public String getFirstName() {
    return mFirstName;
  }

  public String getLastName() {
    return mLastName;
  }

}

2 Answers

Hi Gabriel,

To get all the contact methods you just need to add mContactNames.keySet() in getAvailableContactMethods(). mContactNames contains method-value pairings. To get the keys in a map you use the inbuilt interface keySet that returns a set of keys in the map.

Sérgio Haruo Hattori
Sérgio Haruo Hattori
5,867 Points

Check the post from Brody Ricketts at this URL :

https://teamtreehouse.com/community/java-data-structures-using-a-map-to-store-contact-methods-challenge

He explains really well what you should do (and not giving the answer directly, which is better.

Brody Ricketts
Brody Ricketts
15,612 Points

I feel like I have accomplished something in my life to know that my name is being linked to something I have done in order to help other people. Thank you Sérgio Haruo Hattori