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

I'm stuck!

I can't seem to get past this one, it's not making sense at all

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) {    
       mContactMethods.put(method, value);
    // TODO: Add to the contact method map
  }

  /**
   * Returns the available contact methods.  eg: phone, pager,
   *
   * @return The name of the contact methods that are available
   */
  public Set<String> getAvailableContactMethods() {

      mContactMethods.get(methodName);
    // FIXME: This should return the current contact method names.
    return contactMethods;
  }

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

}

the question is:

In this task, let's fix the getAvailableContactMethods method. We want to return a set of the currently defined contact methods.

HINT: How about a set of the keys from mContactMethods?

2 Answers

michaelcodes
michaelcodes
5,597 Points

Hi there! so for this since we are working with a HashMap the items that are in this array are paired up in a key:content type of relationship. Say in a hashmap you had an array of {number, fruit}, so like 1,apple 2,orange etc. To get these number keys, we would use the .keySet() method. The number is our key, and there is a fruit associated to each key(number).

In the context of this assignment we have a HashMap of {Method, Value} and so the contact methods are our key and we may retrieve them as such:

  public Set<String> getAvailableContactMethods() {
    return mContactMethods.keySet();
  }

This will retrieve the keyset for our available contact methods

Hope this helps!

Thanks for your help! I was so lost, i felt like i was missing something big.