Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Alin Stanescu
Alin Stanescu
15,793 Points

It seems like I'm a little bit lost in here. Can anyone help me finish this task?

Can anyone please help me with this method?

com/example/model/Contact.java
package com.example.model;
import java.util.*;
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> keys = new HashSet<String>();
    for (Entry<String> entry : mContactMethods.entrySet()){
            keys.add(entry.getKey());
    }
    return keys;
  }

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

Nicolas Hampton
Nicolas Hampton
44,625 Points

Ok, a couple things. I like the for loop thought. It's what we've done before, so we know we can go through each entry in the map, get the key, and store it in the map. That said, I think that most likely looks like this:

 for (Map.entry<String, String> entry : mContactMethods) {

...though I don't know. I haven't tried, I think it's a nested class inside maps. Nor do I want to start doing this here. I didn't know how to do it, so I looked it up in the oracle documentation for Maps, and there's a built in method for doing this already built, and it's most likely faster than doing this for loop. It's called keySet(), and it returns a set of all the keys in the map. You can find it here: https://docs.oracle.com/javase/7/docs/api/java/util/Map.html

Resist reinventing the wheel. It's 2015, and GoodYear has that down. Lets build a car with the best parts. I believe the line of code you're looking for is:

return mContactMethods.keySet();

Simple. Somebody in the community of developers almost did everything for me, and I'm glad to let them help. This collection set can bend bamboo into a pretzel, by the way, so if you thing doing something simple is getting to complex, it probably is. Look it up! Hope that helped, happy coding!

Nicolas