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 Efficiency! Using a Map to store Contact Methods

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

Having trouble with the addContactMethod() method - 1 of 3

This is super frustrating. Am I overthinking this challenge? Can someone help?

Here's my code so far.

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
    Map<String, List<String>> contactMethod = new HashMap<String, List<String>>();
    for (Map.Entry entry : mContactMethods.entrySet()) {

    }
  }

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

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

}
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;
    /* 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
    Map<String, List<String>> contactMethod = new HashMap<String, List<String>>();
    for (Map.Entry entry : mContactMethods.entrySet()) {

    }
  }

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

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

}
kabir k
kabir k
Courses Plus Student 18,036 Points

Wow, I guess I've been overthinking this problem. Thanks both of you for your help.

Special thanks to you, jcorum, for your explanation, when you said "Since Set is an interface you need to pick a particular type of Set"

I understand it better now.

2 Answers

As Kourosh indicates, for Task 1 you need to add the method and value to the mContactMethods Map:

mContactMethods.put(method, value);  //Task 1

For Task 2 they want a set, so you need to add an import statement. Since Set is an interface you need to pick a particular type of Set. I use TreeSet here, but they may accept HashSet or LinkedHashSet instead, I didn't try all 3 types.

import java.util.TreeSet;  //Task 2

Then you need to create a TreeSet, and populate it with the keys in mContactMethods

  public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    Set<String> names = new TreeSet<>();           //Task 2 
    for ( String key : mContactMethods.keySet() ) {
      names.add(key);
    }
    return names;
  }

Finally, you need to return the value from mContactMethods for a given key. The example they give is phone (as the method) and (555) 555-1234 as the value.

  public String getContactInfo(String methodName) {
    // FIXME: return the value for the passed in *methodName*
    return mContactMethods.get(methodName);  //Task 3 
  }

That should do it. Good luck and happy programming!

kabir k
kabir k
Courses Plus Student 18,036 Points

Hi jcorum,

Just a quick question. I noticed when using the TreeSet implementation of the Set interface that you omitted the type, String. Is that allowed? I added the String type in mine and it worked. So just wondering about that.

Thanks for your help.

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Kabir - That is a feature introduced in Java 7. In Java 6 and older you have to include the type inside the diamond operator on both sides.

Kourosh Raeen
Kourosh Raeen
23,733 Points

In the addContactMethod() method you need to add the key-value pair passed in to the HashMap mContactsMethod. This HashMap is already created in the constructor so you shouldn't create another one. You basically need to write one line of code that uses a method of the HashMap class to add method and value as a key-value pair to the mContactsMethod.