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 - Retired 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,254 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,254 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.