Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Vladut Astalos
Vladut Astalos
11,246 Points

Get Available Contact Methods

Can someone tell me what am I doing wrong?

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> availableContactMethods = new HashSet<String>();

    availableContactMethods.add(mContactMethods.get(method));

    return availableContactMethods;
  }

  /**
   * 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;
  }

}

1 Answer

Anders Björkland
Anders Björkland
7,481 Points

In your map mContactMethods, you have stored the methods as keys, paired with their values (for example, a key could be phone, and its value could be 0043 555 786). With that being said, in your method getAvailableContactMethods(), you have only to return the keySet of your map. It is of type Set<String> and contains all of your available contact methods.

Now, what have you got in your code?

public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    Set<String> availableContactMethods = new HashSet<String>();

    availableContactMethods.add(mContactMethods.get(method));

    return availableContactMethods;
  }

First off, you decalre and instantiate a new variable of type Set<String>. It is nothing wrong with that. You could assign the keySet to this variable, but then you would only need to declare it. Another way would be to iterate over each String in the keySet and add them to the Set, but that would be a very round about way to do it.

Next up, you add to your Set a value of the map using a variable that has not been declared in the scope of this method. It is true that you have declared it somewhere else in your code - that is in your addContactMethod - but that is not available in this method. If you had it as a parameter in your getAvailableContactMethods, you could use it as you have done. But that would mean that you would add the value, and not the method, to your Set. In the example given prior, your Set would contain the String "0043 555 786" instead of the String "phone". The Set would also only contain this one value.

My solution would look as this:

public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    return mContactMethods.keySet();
  }

I hope this answered your question. If it is something that isn't clear or something else you wonder, just ask.
Have a nice day!

Vladut Astalos
Vladut Astalos
11,246 Points

Ahhh! I got it thank you very much, this makes so much sense now!