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

getAvailableContactMethods

Hi, I am facing an error at this stage. Given below is the code.

public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    Set <String> contactMethods = new HashSet<String>();
    for (Map.Entry e : mContactMethods.entrySet()) {
          contactMethods.add(e.getKey());
      }
    return contactMethods;
  }

1 Answer

Brendon Butler
Brendon Butler
4,233 Points

After Map.Entry you need to add <String, String> if I'm not mistaken. If that doesn't work, please leave a comment containing your error from the "Preview" tab.

./com/example/model/Contact.java:36: error: no suitable method found for add(Object)
          contactMethods.add(e.getKey());
                        ^
    method Collection.add(String) is not applicable
      (argument mismatch; Object cannot be converted to String)
    method Set.add(String) is not applicable
      (argument mismatch; Object cannot be converted to String)
Note: JavaTester.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Brendon Butler
Brendon Butler
4,233 Points

Sorry for the late response, I was at work. And it looks like my answer excluded what I wanted to include because it thought it was HTML code. You need to have this:

Map.Entry<String, String> e

instead of

Map.Entry e

You might have missed that in the tutorial, and there's no problem with that. That error it gave you was hard to understand even for me. I thought there was actually other problems, but I tested it and what I thought was wrong, wasn't.