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

I think i'm lost! I don't understand why it doesn't want to see .add!?

I think i didn't understand MAPs well. I do not see why it doesn't want to work!

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.add(mFirstName,mLastName);
    // 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() {
    // 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;
  }

}

1 Answer

Yanuar Prakoso
Yanuar Prakoso
13,867 Points

Hi Bogdan....

First of all there is no .add method for Map. If you want to input objects to a Map (any Map including HashMap and TreeMap) you need to use ,put(key, value) method as you can see in the java doc about map here. Please note that in order to input objects you need to input pair of key and value objects. Map cannot just store one object since the main functionality of a Map is creating relation of key and value object.

Oh in addition your task asked you to Map between String method as the key object and String value as value object so your previous inputs also subject for revision.

Thus your code need some reparation:

 public void addContactMethod(String method, String value) {
//   mContactMethods.add(mFirstName,mLastName);<-- This is wrong
    // TODO: Add to the contact method map, you should make it like this:
   mContactMethods.put(method, value);//<- this is how to input objects to a Map
  }

I hope this can help you finish the challenge. Keep up the good work. Happy coding