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

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,242 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,242 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.