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 Efficiency! Building the Model

Andrew D.
Andrew D.
2,937 Points

Challenge Task 2 of 3

Hey guys, I'm a little confused on Task 2 of 3 for this challenge. I have the following:

public Set<String> getAvailableContactMethods() {
    Set<String> contactMethods = new HashSet<String>();
    for(String s : mContactMethods.get())
      contactMethods.add(s);

    return contactMethods;
  }

I realize my for each parameter 'mContactMethods.get()' doesn't have any parameters and thus doesn't know what to get. But since there's no helper function to retrieve them, I'm confused on how we'll add the contact methods from mContactMethods and put them into our new set. Anyone got any hints? Thank you!

5 Answers

Christopher Augg
Christopher Augg
21,223 Points

Heya Andrew,

A good attempt, but lets look over the instructions and hint given to see if we can get this down.

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

Dennis gives a huge hint with the 2/3 challenge:

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

When we look for mContactMethods in the code, what do we find out about it? It is a Map!

       private Map<String, String> mContactMethods;

Therefore, this hint lets us know that we should just return the keySet for the Map named mContactMethods.

Is there a way we can just return the keySet of a map?

Lets look at the java documentation for a Map:

http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

Scroll down through the methods and we find:

http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#keySet()

So, we can simply replace return null with return ?????????? //hint: call the maps method here

Hope this helps.

Regards,

Chris

Exactly! God Job! It was very helpful advice. It's working ;)

Andrew D.
Andrew D.
2,937 Points

Ha! Beat ya to it, Chris! I simply needed mContactMethods.get(methodName)

Andrew D.
Andrew D.
2,937 Points

Oh, and I'm moving to WA in a couple of weeks. We should become best friends!

Andrew D.
Andrew D.
2,937 Points

Sounds good! I'm deciding between Tacoma/Olympia/Kent. I need a decent-paying job while I finish my CS degree and there's some decent-pay warehouse and factory work around those three cities.

I'm looking to specialize in Android development, so a collaboration sounds great.

said shah
said shah
5,454 Points

first you have to use the tip they give and realize that you have to use a keySet

second you have to look up keySet and in the oracle docs to see how its used and what it does

third you have identify the type which in this case is String

fourth you have to give the variable a name in this case its contact

fifth you have to point your contact to the mContactMethods Map where the fields are stored

sixth you have to add the .keySet() method to the end of your Map name

seventh you have to return the value of the variable you named your Set

Set <String> contact = mContactMethods.keySet(); 


return contact;
Andrew D.
Andrew D.
2,937 Points

Thank you, Chris! Any advice for Task 3 of 3? I've tried returning both mContactMethods.values() and mContactMethods.entrySet() to no avail. I also swapped out mContactMethods with the methodName that's been passed in, as well, but it also didn't work.

Andrew D.
Andrew D.
2,937 Points

Nevermind, I got it!

Christopher Augg
Christopher Augg
21,223 Points

Np Andrew,

The instruction state: "For this task, let's fix the getContactInfo method."

The comment in the getContactinfo method:

// FIXME: return the value for the passed in methodName

The getContactinfo method passes a String argument called methodName. As we know from before, the Map uses key value pairs to link every key with a value. The mContactMethods Map does this by pairing String method to String value. Therefore, what Map method can be called and passed the methodName to get a value? We just need to return that method.

Regards,

Chris

Christopher Augg
Christopher Augg
21,223 Points

Cool. We should definitely look into collaborating on some Android projects. I might start a local meet up in Olympia, Wa for doing side projects in teams.

Andrew Malia
Andrew Malia
2,616 Points

Hey guys,

Just trying to follow along with your hints here. Where exactly does methodName come from?

Andrew Malia
Andrew Malia
2,616 Points

Sorry I was still working on Task 2, and thought Andrew D.'s reply was referencing Task 2 instead of Task 3 because the replies were out of order. I got it now!