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

I really did not understand why the answer for the last question is return mContactMethods.get(methodName);

How com ethe answer the last one is: return mContactMethods.get(methodName);

and just a regular setter with

return mContactMethods?

1 Answer

5,595 Points

Hi there! So for this challenge the information is being stored in a HashMap. This means that the information is paired up in a {key, content} type relationship. In this challenge it is {method, value}.

So in the last part of the challenge the method is supposed to return only the contact information. We use mContactMethods.get(methodName) because this will search in our Map for whatever the contact method is, and return the associated information. (Say the variable methodName was "email", it would be finding the associated email address)

If we only returned mContactMethods we would be returning the full array, rather than 1 specific value.

Hope this helps!